Reputation: 26476
Using the Postman extension for Chrome I can successfully POST some JSON. Using Charles to inspect the request, I see that the request data is as follows:
{
"query": {
"term": {
"user_id": "12345"
}
}
}
When I try to construct this same request using AFNetworking 2.4.1, I can see that the data is formatted as:
query[term][user_id]=12345
The server of course returns an error.
What part of the POST request am I getting wrong?
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"query":@{@"term":@{@"user_id":@"12345"}}};
[manager POST:@"http://someURL" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"SUCCESS %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"FAIL: %@", error);
}];
Upvotes: 1
Views: 221
Reputation: 26476
The short answer is:
manager.requestSerializer = [AFJSONRequestSerializer serializer];
From the documentation:
Requests created with
requestWithMethod:URLString:parameters:
&multipartFormRequestWithMethod:URLString:parameters:constructingBodyWithBlock:
are constructed with a set of default headers using a parameter serialization specified by this property. By default, this is set to an instance ofAFHTTPRequestSerializer
, which serializes query string parameters forGET
,HEAD
, andDELETE
requests, or otherwise URL-form-encodes HTTP message bodies.
Upvotes: 1