21

I am using AFNetworking library to post data on server using POST method.

Following is my code

- (void) callLoginAPI:(NSDictionary *)dictProfile{
    // 1
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:[dictProfile valueForKey:@"name"], @"username",
                                                                    [dictProfile valueForKey:@"first_name"],@"first_name",
                                                                    [dictProfile valueForKey:@"last_name"],@"last_name",
                                                                    [dictProfile valueForKey:@"email"],@"email",
                                                                    [dictProfile valueForKey:@"birthday"],@"dob",
                                                                    [dictProfile valueForKey:@"gender"],@"gender",
                                                                    [[dictProfile valueForKey:@"location"] valueForKey:@"name"],@"location",
                                                                    [dictProfile valueForKey:@"timezone"],@"timezone",
                                                                    @"",@"language",
                                                                    [NSString stringWithFormat:@"http://graph.facebook.com/%@/picture?type=large",[dictProfile valueForKey:@"id"]],@"profile_pic_url",
                                                                    @"",@"cover_pic_url",nil];



    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];



    [manager POST:@"http://10.1.81.35:8000/api/login/" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}

but I got following error in response

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=0x797f2620 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

I can't understand what is the problem with the code.

user1954352
  • 1,577
  • 4
  • 14
  • 16

3 Answers3

45

The problem comes from response parsing. You are trying to de-serialize a JSON reponse (which MUST be contained in either a NSArray or NSDictionary) however your response is none of the above (Most likely a simple string).

Also, try to set the "allow fragments" to the response serializer.

AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
Vlad
  • 3,346
  • 2
  • 27
  • 39
14

May be you need authentication to access JSON response. Set authentication like that:

[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"XYZ" password:@"xyzzzz"];

Try this:

AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
[self setResponseSerializer:responseSerializer];

instead of:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
pankaj asudani
  • 862
  • 6
  • 18
  • Great answer showing where this code needs to be input. A small detail but very important – sam_smith Jun 01 '15 at 11:49
  • I was stuck on this issue for a while, tried everything in this answer -> http://stackoverflow.com/questions/19114623/request-failed-unacceptable-content-type-text-html-using-afnetworking-2-0 but your answer was the one that worked. – Allen Apr 22 '16 at 17:09
  • @iDeveloper Thanks it worked for me... struggling with it for a while... 1 upvote – Abirami Bala Feb 16 '17 at 09:44
  • @iDeveloper Please tell me what is [self setResponseSerializer:responseSerializer]; as i get error here – Abha Oct 09 '17 at 10:00
0
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

//Request Serializer
manager.requestSerializer = [AFJSONRequestSerializer serializer];

//Response Serializer
AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
manager.responseSerializer = responseSerializer;
Pranit
  • 892
  • 12
  • 20