AFNetworking 2.0: Cannot POST with JSON from iOS

Since updating my iOS code to use AFNetworking version 2.0 instead of 1.x, I can no longer execute an HTTP message with JSON parameters.

I suspect this is because my HTTP request header is not "application / json", but I tried to do this and it still does not work.

This is my test code for trying POST from JSON:

NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys: email, @"email", password, @"password", nil]; [[OTApiClient sharedInstance] POST:@"login" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"result: %@", responseObject); Boolean response = [[responseObject valueForKey:@"success"] boolValue]; if(response == TRUE) { //ok, success NSLog(@"success"); UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Success!" message:@"Successfully logged in" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; } else { NSLog(@"Not successful"); UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"The email or password is incorrect." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; } dispatch_async(dispatch_get_main_queue(), ^{ [MBProgressHUD hideHUDForView:self.view animated:YES]; }); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"error: %@ , for operation: %@", error, operation); UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"There is a problem connecting to the server, please try again soon." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; dispatch_async(dispatch_get_main_queue(), ^{ [MBProgressHUD hideHUDForView:self.view animated:YES]; }); }]; }); 

and I get this error:

 Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn't be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0xca80730 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.} , for operation: <AFHTTPRequestOperation: 0xca6bee0, state: isFinished, cancelled: NO request: <NSMutableURLRequest: 0xca76040> { URL: http://1.2.3.4:9000/api/login }, response: <NSHTTPURLResponse: 0xc963d60> { URL: http://1.2.3.4:9000/error404 } { status code: 404, headers { "Content-Length" = 1809; "Content-Type" = "text/html; charset=utf-8"; 

}}>

I am very sure that the server is accessible and accessible, because all other GET calls work correctly.

I checked the updated documentation for AFNetworking 2.0 and it seems that I have the correct way to do JSON POST

Please let me know if I missed anything.

Thanks IS

+6
source share
3 answers

Actually, I finally got it for work.

Basically, for my request, the AFHTTPRequestOperationManager Serializer, I used the AFHTTPRequestSerializer and then tried to set the Content-Type to "application / json"

But as soon as I switched to using AFJSONRequestSerializer, now it works fine

 AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer]; [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"]; operationManagerInstance.requestSerializer = requestSerializer; 
+22
source
 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer]; [serializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [serializer setValue:@"application/json" forHTTPHeaderField:@"Accept"]; manager.requestSerializer = serializer; 

Now it will work.

+5
source

I ran into the same problem using the AFNetworking API via Swift, trying to send a mail request and finally received it also thanks to message user1805458.

Basically, for my request, the AFHTTPRequestOperationManager Serializer, I also used the AFHTTPRequestSerializer and then tried to set the Content-Type to "Application / JSON" and it did not work.

But as soon as I switched to using AFJSONRequestSerializer, now it works fine:

  let manager = AFHTTPRequestOperationManager() manager.requestSerializer = AFJSONRequestSerializer(writingOptions: nil) // Contrary to user1805458 solution, I did not need to use these two instructions below using Swift. // manager.requestSerializer.setValue("Application/JSON", forHTTPHeaderField:"Content-Type") // manager.requestSerializer.setValue("Application/JSON", forHTTPHeaderField:"Accept") 

I hope this helps you fix this error if you use Swift, AFNetworking and try to send a request for a message containing the JSON body.

+1
source

Source: https://habr.com/ru/post/959342/


All Articles