AFNetworking 2.0: is it possible to put pure json in the body of a POST request?

I would like to make the following request from my application:

AFHTTPRequestOperationManager *requestManager = [[AFHTTPRequestOperationManager alloc] init]; requestManager.responseSerializer.acceptableContentTypes = [requestManager.responseSerializer.acceptableContentTypes setByAddingObject:@"application/json"]; requestManager.requestSerializer = [AFJSONRequestSerializer serializer]; [requestManager POST:urlString parameters:aParameters constructingBodyWithBlock:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"%@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"error: %@", error); }]; 

Where aParameters is an NSDictionary with the following content:

 NSDictionary *urlParams = @{@"username" : anUser.userName, @"password" : anUser.password}; 

When I make a request from my application with user input "anUsername" and "aPassword", I get the following log for the body in my servlet:

 --Boundary+5738A89B2C391231 Content-Disposition: form-data; name="password" aPassword --Boundary+5738A89B2C391231 Content-Disposition: form-data; name="username" anUsername --Boundary+5738A89B2C391231-- multipart/form-data; boundary=Boundary+5738A89B2C391231 

I got the impression that using AFJSONRequestSerializer would send my request in the appropriate format, but as the log shows this, it multipart / form data. It is very difficult for me to parse this request (I parse it in Java on the server side), so my question is: is it possible to send json to the body of my request? Something like that:

 { "userName" : "anUsername", "password" : "aPassword" } 

Any help would be appreciated.

+6
source share
2 answers

For all interested parties: instead of using the POST:parameters:constructingBodyWithBlock:success:failure: method, you should use POST:parameters:success:failure: The former performs a multiple form request, while the latter encodes a url. In addition, to send parameters to JSON, the requestSerializer property of an requestSerializer instance must be an AFJSONRequestSerializer instance (by default it is set to AFHTTPRequestSerializer ) It is very useful to look at the AFHTTPRequestSerializer implementation AFHTTPRequestOperationManager for details, this helped me deal with this error.

+5
source

You do not need to send pure JSON to the POST request, just send the parameter dictionary. Here is an example of code that works for a POST request.

 + (void)login:(BOUser *)user responseBlock:(APIRequestResponseBlock)responseBlock { AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer = [AFJSONResponseSerializer serializer]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; [manager.requestSerializer setValue:@"parse-application-id-removed" forHTTPHeaderField:@"X-Parse-Application-Id"]; [manager.requestSerializer setValue:@"parse-rest-api-key-removed" forHTTPHeaderField:@"X-Parse-REST-API-Key"]; [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; manager.securityPolicy.allowInvalidCertificates = YES; NSString *URLString = [NSString stringWithFormat:@"%@login", BASE_URL_STRING]; NSDictionary *params = @{@"email": user.username, @"password": user.password}; [manager POST:URLString parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"JSON: %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); responseBlock(nil, FALSE, error); }]; } 

Hope this helps.

+3
source

All Articles