1

I am trying to post a JSON message which contains an array to my server. However the server is unable to identify the elements in the array. After inspecting the issue I have come across what I believe is the problem.

AFNetworking code:

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:kServiceBaseUrl]];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

[manager PUT:@"/api/createRubaway"
   parameters:myDictionary
      success:^(NSURLSessionDataTask *task, NSDictionary *responseObject)
     {/* stuff */}
      failure:^(NSURLSessionDataTask *task, NSError *error)
     {/* stuff */}
 ];

LLDB print out of 'myDictionary':

{
    description = description;
    title = title;
    transitions =     (
                {
            idx = 0;
            resource =             {
                data = testdata;
                name = transition;
                type = image;
            };
            threshold = "0.75";
            type = rubaway;
        },
                {
            idx = 1;
            resource =             {
                data = testdata;
                name = transition;
                type = image;
            };
            threshold = "0.75";
            type = rubaway;
        }
    );
}

After AFNetworking has serialized the request. If I print out the HTTPBody from the NSMutableRequest, this is what it is sending (this is what is being sent to my server):

description=description&title=title&transitions[][idx]=0&
transitions[][resource][data]=testdata&
transitions[][resource][name]=transition&
transitions[][resource][type]=image&
transitions[][threshold]=0.75&
transitions[][type]=rubaway&transitions[][idx]=1&
transitions[][resource][data]=testdata&
transitions[][resource][name]=transition&
transitions[][resource][type]=image&transitions[][threshold]=0.75&
transitions[][type]=rubaway

Is the above formatting correct. As my server (Node.js - BodyParser) I believe is expecting it in this format https://stackoverflow.com/a/18402556/623750

Is there a way to change the formatting of the JSON serializer?. Alternatively is there a way to configure BodyParser to accept the above formatting?

Thanks, Ryan

Community
  • 1
  • 1
Ryan
  • 865
  • 2
  • 10
  • 17
  • a question, in the nodejs code; what did you add as require('') to the file? I'm facing the same problem, the req.body is not recognised as an array. – zoma.saf Feb 27 '15 at 11:08

1 Answers1

1

All you need to do, to set requestSerializer to AFJSONRequestSerializer, this will fix the issue.

 [manager setRequestSerializer:[AFJSONRequestSerializer serializer]];
 [manager setResponseSerializer:[AFJSONResponseSerializer serializer]];
l0gg3r
  • 8,864
  • 3
  • 26
  • 46
  • Ah. I forgot to set the request serializer. Thanks, works great! – Ryan Oct 03 '14 at 11:21
  • req.body is equal to the afnetworking response object. If you want an array then: req.body = ['test','test2']; – Ryan Feb 27 '15 at 11:17