AFNetworking 2.0 POST, Cocoa error 3840 error (JSON text did not start with an array ...)

I am trying to call api.php on my local server (using MAMP). The server side api.php is called, but the contents of _POST inside the php code contains the following 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 = 0x15d7bdd0 {NSDebugDescription = JSON text did not start with array or object and option to allow fragments not set.} 

My app that is trying to send a JSON POST request to api.php is an iOS app using AFNetworking 2

This is my request code:

 - (void)postUpdateRequest { if (!dataModel) dataModel = [[DataModel alloc] init]; NSDictionary *params = @{@"foo": @"bar2"}; NSLog(@"%@",params); /* NSDictionary *params = @{@"cmd":@"update", @"user_id":[dataModel userId], @"token":[dataModel deviceToken] };//@"ip_address":[dataModel getIPAddress]};*/ AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; // manager.requestSerializer = [AFJSONRequestSerializer serializer]; [manager POST:ServerApiURL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"JSON: %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }]; } 

I have looked all over the Internet but cannot find the answer to my specific problem. No matter what I do, I get the same error.

Why is my JSON not formatted properly? I also cannot find a way to smell what JSON I am really posting.

Thanks.

+6
source share
3 answers

When you use the API, as in your example, your HTTP message will be compiled using the Content-Type application/x-www-form-urlencoded . Internally, your param dictionary is AFN encoded (although it is not strictly correct, as indicated by w3c), and is set as the request body.

Since you did not specify an Accept header, the server can select the content type for possible responses (if any).

When you receive a response, you should always check the HTTP status code and the type of content of the response body (if any).

The server probably returned a status code indicating some kind of problem, and the response body containing the "error response" in the form of a certain type of content than you expect (for example, it returned text / html).

+5
source

You can use smth like wireshark ( http://www.wireshark.org/ ) while the application is running in the simulator or on a device connected via shared Wi-Fi on a Mac, you run whireshark to track the actual requests and responses.

+2
source

This works for me:

  AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer]; [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"]; manager.requestSerializer = requestSerializer; [manager POST:URLString parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"JSON: %@", responseObject); [MBProgressHUD hideAllHUDsForView:self.view animated:YES]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); [MBProgressHUD hideAllHUDsForView:self.view animated:YES]; }]; 
+2
source

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


All Articles