Nested NSDictionary and NSArray with AFNetworking do not work properly

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 the Node.JS server, printing the body displays:

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

The expected print should be:

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

What am I doing wrong?

+4
source share
1 answer

From the AFNetworking wiki page:

How to send JSON parameters to my request?

If you use AFHTTPClient, set parameterEncoding to AFJSONParameterEncoding. Any method on this HTTP client with argument arguments now encodes the passed object in a JSON string and adjust the HTTP header and Content-Type header accordingly.

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

+7
source

Source: https://habr.com/ru/post/1442435/


All Articles