2

I am trying to use AFNetworking 2.0 on iOS7 to send a JSON-encoded array via POST to a web server.

_manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:baseURL]];
_manager.requestSerializer = [AFJSONRequestSerializer serializer];

[_manager POST:@"getlistings" parameters:@{@"listings":item_ids} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"%@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"%@",error);
}];

item_ids is an array which is passed in, it's essentially @[1234,1235], just a few numbers. The web service which receives this request doesn't have anything set for $_POST, but the JSON I'm sending does show up in $HTTP_RAW_POST_DATA. Why is this? Am I doing something wrong with my request, or is it more likely that there is a problem on the server side?

Thanks in advance!

Wernzy
  • 1,088
  • 8
  • 22

1 Answers1

1

Your AFNetworking code looks fine, if what you want is for your server to parse JSON in the response.

I'm no PHP expert, but according to this answer, "$_POST will not be populated if the request body is not in the standard urlencoded form."

So, either follow the instructions in that answer to parse JSON, or change your requestSerializer to an instance of AFHTTPRequestSerializer, which uses standard URL encoding, instead of AFJSONRequestSerializer.

Community
  • 1
  • 1
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287