POST request with AFNetworking 2.0 - AFHTTPSessionManager

Hey

I am struggling with making a POST request to parse a REST API. I am using AFNetworking 2.0. My code for a subclass of AFHTTPSessionManager is as follows:

+ (ParseAPISession *)sharedSession { static ParseAPISession *sharedSession = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedSession = [[ParseAPISession alloc] initWithBaseURL:[NSURL URLWithString:kSDFParseAPIBaseURLString]]; }); return sharedSession; } 

and

 - (id)initWithBaseURL:(NSURL *)url { self = [super initWithBaseURL:url]; if (self) { [self.requestSerializer setValue:kSDFParseAPIApplicationId forHTTPHeaderField:@"X-Parse-Application-Id"]; [self.requestSerializer setValue:kSDFParseAPIKey forHTTPHeaderField:@"X-Parse-REST-API-Key"]; } return self; } 

I am making a request as follows:

 [[ParseAPISession sharedSession] POST:@"ClassName" parameters: [NSDictionary dictionaryWithObjectsAndKeys:@"name", @"name", nil] success:^(NSURLSessionDataTask *task, id abc) { NSLog(@"%@", abc); } failure:^(NSURLSessionDataTask *task, NSError *error) { NSLog(@"%@", error); }]; 

When doing this, I always get this error:

Domain error = NSCocoaErrorDomain Code = 3840 "Operation could not be performed. (Cocoa error 3840.)" (JSON text did not start from the array or object, and the option allowed the removal of fragments.) UserInfo = 0x8c72420 {NSDebugDescription = JSON text did not start from the array or object and options to allow fragments.}

Since the GET request works like a charm, I am completely confused why I cannot do something POST. Can someone stop me with this problem?

Yours faithfully!

UPDATE

Fortunately, after repeated testing, this error message no longer appears, unfortunately, another did not appear:

 <NSHTTPURLResponse: 0x8b96d40> { URL: https://api.parse.com/1/users } { status code: 400, headers { "Access-Control-Allow-Origin" = "*"; "Access-Control-Request-Method" = "*"; "Cache-Control" = "no-cache"; Connection = "keep-alive"; "Content-Length" = 130; "Content-Type" = "application/json; charset=utf-8"; Date = "Wed, 30 Oct 2013 20:01:58 GMT"; Server = "nginx/1.4.2"; "Set-Cookie" = "_parse_session=BAh7BkkiD3Nlc3Npb25faWQGOgZFRiIlNjIxZjUxMzY3NWVhZWJmMDYyYWYwMGJiZTQ3MThmMWE%3D--851bd31b07e7dba2c5f83bb13a8d801ecbea42c4; domain=.parse.com; path=/; expires=Fri, 29-Nov-2013 20:01:58 GMT; secure; HttpOnly"; Status = "400 Bad Request"; "X-Runtime" = "0.060910"; "X-UA-Compatible" = "IE=Edge,chrome=1"; } } = BAh7BkkiD3Nlc3Npb25faWQGOgZFRiIlNjIxZjUxMzY3NWVhZWJmMDYyYWYwMGJiZTQ3MThmMWE% 3D - 851bd31b07e7dba2c5f83bb13a8d801ecbea42c4; domain = .parse.com; path = /; expires = Fri, <NSHTTPURLResponse: 0x8b96d40> { URL: https://api.parse.com/1/users } { status code: 400, headers { "Access-Control-Allow-Origin" = "*"; "Access-Control-Request-Method" = "*"; "Cache-Control" = "no-cache"; Connection = "keep-alive"; "Content-Length" = 130; "Content-Type" = "application/json; charset=utf-8"; Date = "Wed, 30 Oct 2013 20:01:58 GMT"; Server = "nginx/1.4.2"; "Set-Cookie" = "_parse_session=BAh7BkkiD3Nlc3Npb25faWQGOgZFRiIlNjIxZjUxMzY3NWVhZWJmMDYyYWYwMGJiZTQ3MThmMWE%3D--851bd31b07e7dba2c5f83bb13a8d801ecbea42c4; domain=.parse.com; path=/; expires=Fri, 29-Nov-2013 20:01:58 GMT; secure; HttpOnly"; Status = "400 Bad Request"; "X-Runtime" = "0.060910"; "X-UA-Compatible" = "IE=Edge,chrome=1"; } } 

Can someone tell me what status will tell me: 400 Bad Request and how can I get rid of it?

+6
source share
2 answers

This error means that your POST request has passed, the server successfully returns you some data that NSJSONSerialization having problems analyzing.

You probably need to set AFJSONResponseSerializer to allow JSON fragments.

In the init method of your subclass AFHTTPSessionManager :

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

If this does not work, there is probably a problem with the encoding. From the NSJSONSerialization class NSJSONSerialization :

The data must be in one of the 5 supported encodings listed in the JSON specification: UTF-8, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE. Data may or may not have a specification. The most efficient encoding for parsing is UTF-8, so if you have a choice in encoding the data passed to this method, use UTF-8.

Check the encoding type sent by your server.

Finally, you can either set breakpoints inside AFNetworking, or set up AFNetworkActivityLogger , which will log requests as they are sent and received to your console. This tool is incredibly useful for debugging this type of problem.

+9
source

This worked for me:

in a subclass of AFHTTPSessionManager , it initializes with the following serializers:

[self setRequestSerializer:[AFHTTPRequestSerializer serializer]]; [self setResponseSerializer:[AFJSONResponseSerializer serializer]];

+1
source

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


All Articles