AFNetworking 2.0 POST issue, Cocoa error 3840(JSON text did not start with array...)
Asked Answered
D

3

6

I am attempting to call api.php on my local server(using MAMP). The server side api.php is being called but the content 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 the JSON POST request to the 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 looked all over the internet but can't find an answer for my specific problem. No matter what I do I get the same error back.

Why is my JSON not properly formatted? I also can't seem to find a way to sniff what JSON I am actually sending.

Thank you.

Danieladaniele answered 9/11, 2013 at 10:32 Comment(1)
1) What makes you sure you send JSON data? (actually, you don't). 2) What do you expect the server returns? (probably not what you expect) 3) Where is the line of code which causes the error?Exclude
E
5

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

Since, you did not specify an Accept header, the server is free to choose the content type for a possible response data (if any).

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

It's likely, the server returned a status code indicating some issue and a response body containing an "error response" in some different kind of content type than you expect (for example, it returned text/html).

Exclude answered 9/11, 2013 at 10:53 Comment(1)
The issue was that I was actually trying to put _POST in the error message but it just complained on the error format, I think. I did something like exitWithError(400, $_POST); I mistakenly thought I was printing the _POST content.Danieladaniele
S
2

You may use smth like wireshark (http://www.wireshark.org/) while running app in simulator or on device connected via shared wifi to mac you're running whireshark on to trace actual requests and responses.

Screed answered 9/11, 2013 at 10:59 Comment(0)
I
2

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];
          }];
Individualism answered 13/1, 2016 at 11:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.