4
NSDictionary *customerDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"blah@blah.com", @"email", @"1", @"facebook", nil];
NSArray *customerArray = [NSArray arrayWithObjects:customerDictionary, nil];
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:customerArray, @"customers", nil];
NSURLRequest *request = [sharedHTTPClient requestWithMethod:@"POST" path:@"/api/upload" parameters:parameters];
AFJSONRequestOperation *operation =
    [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                    success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {}
                                                    failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {}];
[operation start];

On a Node.JS backend, printing out the body shows:

{ customers: [ '1', 'blah@blah.com' ] }

Expected print should be:

{ customers: [{ facebook:'1', email:'blah@blah.com' }] }

What am I doing wrong?

Kjuly
  • 34,476
  • 22
  • 104
  • 118
ninjaneer
  • 6,951
  • 8
  • 60
  • 104

1 Answers1

7

From the AFNetworking wiki:

How do I send JSON parameters in my request?

If you're using AFHTTPClient, set the parameterEncoding property to AFJSONParameterEncoding. Any method on that HTTP client with a parameters argument will now encode the passed object into a JSON string and set the HTTP body and Content-Type header appropriately.

Otherwise, you can do this manually by adding the header Content-Type: application/json, and setting the body of your request to a JSON string.

Community
  • 1
  • 1
Felix
  • 35,354
  • 13
  • 96
  • 143
  • Geez, no matter how many times I've read the wiki, I somehow skipped this part. This did it for me! Thanks. (can't award bounty until 11 hours). – ninjaneer Oct 29 '12 at 19:05