Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 app:

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 the request from my app with the user input of "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 was under the impression that using AFJSONRequestSerializer would send my request in the appropriate format, but as the log shows, it's multipart/form data. It is really hard (for me) to parse this kind of request (I'm parsing it in Java on the server side), so my question is: is it possible to send a json in the body of my request? Something like this:

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

Any help would be appreciated.

like image 619
József Vesza Avatar asked Mar 18 '14 14:03

József Vesza


2 Answers

For anyone concerned: Instead of using the POST:parameters:constructingBodyWithBlock:success:failure: method, you should use POST:parameters:success:failure:. The former performs a multipart form request, while the latter does url form encoding. Additionally, to send the params in JSON, the requestSerializer property of the AFHTTPRequestOperationManager instance should be an instance of AFJSONRequestSerializer (by default it is set to AFHTTPRequestSerializer) It is really helpful to browse the implementation file of AFHTTPRequestOperationManager for details, it helped me sort this error out.

like image 184
József Vesza Avatar answered Oct 30 '22 07:10

József Vesza


You don't need to send pure JSON in POST request, just send Parameters dictionary. Here is the sample code that is working for 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);
    }];

}

I hope it helps.

like image 40
i.AsifNoor Avatar answered Oct 30 '22 08:10

i.AsifNoor