AFNetworking sends an array in JSON parameters of the mail request

I am trying to send parameters to my server through POST, and it works in general, but I cannot figure out how to send JSON, which contains an array as one of the parameters. Here is what I tried:

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:myURL]]; NSMutableArray *objectsInCart = [NSMutableArray arrayWithCapacity:[_cart count]]; for(NSDictionary *dict in _cart) { NSObject *object = [dict objectForKey:@"object"]; NSDictionary *objectDict = @{@"product_id": [NSString stringWithFormat:@"%d",[object productID]], @"quantity": [NSString stringWithFormat:@"%d", [[dict objectForKey:@"count"] intValue]], @"store_id": [NSString stringWithFormat:@"%d", [Store getStoreID]], @"price": [NSString stringWithFormat:@"%.2f", [object price]]}; [objectsInCart addObject:objectDict]; } NSError *error = nil; NSString *cartJSON = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:objectsInCart options:NSJSONWritingPrettyPrinted error:&error] encoding:NSUTF8StringEncoding]; if(error) { NSLog(@"Error serializing cart to JSON: %@", [error description]); return; } NSDictionary *parameters = @{@"status": @"SUBMITTED", @"orders": cartJSON}; NSMutableURLRequest *orderRequest = [httpClient requestWithMethod:@"POST" path:@"/app/carts" parameters:parameters]; AFJSONRequestOperation *JSONOperation = [[AFJSONRequestOperation alloc] initWithRequest:orderRequest]; 

However, when sending this JSON I get an error. Any suggestions are greatly appreciated!

+6
source share
1 answer

I don’t see where you indicate that you want to publish JSON, so I'm sure that you are sending the URL encoding of the form, which goes like this, according to the AFHTTPClient documentation:

If a couple of query lines has an NSArray for its value, each member of the array will be represented in the format field[]=value1&field[]=value2 . Otherwise, the pair will be formatted as "field = value". String representations of both keys and values ​​are output using the -description method. Does the query string string not include? character used to delimit the request component.

If your server really expects you to publish JSON, add it to the second line to tell AFNetworking that:

 // AFNetworking 1.0 // httpClient is a subclass of AFHTTPClient httpClient.parameterEncoding = AFJSONParameterEncoding; // AFNetworking 2.0 // httpClient is a subclass of AFHTTPRequestOperationManager or AFHTTPSessionManager httpClient.requestSerializer = AFJSONRequestSerializer; 

Then you remove your NSJSONSerialization call and just put objectsInCart in the parameters dictionary.

Note: this is normal for subclassing AFHTTPRequestOperationManager or AFHTTPSessionManager (AFNetworking 2.0) or AFHTTPClient (AFNetworking 1.0), and put this type of configuration in your initWithBaseURL: . (You probably do not want to deploy a new client for each request.)

+9
source

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


All Articles