1

So I'm trying to do a post request with an array of JSON parameters sent to a server, here's the code for that

    for(USER_ACTIONS *ua in [USER_ACTIONS listRegisterdActions]){
        //Create a single JSON object here
         [array addObject:jsonString];
     }
     NSString *dataString = [NSString stringWithFormat:@"[%@]",array.count ?      

     NSData* data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
     NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
     parameters[@"data"] = data;

     [self POST:@"?cmd=log" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {

     } failure:^(NSURLSessionDataTask *task, NSError *error) {

     }];

This works with a single JSON object, but once there are more of them I get the following exception

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (NSConcreteMutableData)'

It's out of the question to do this with a multiple post requests so I need a way to do this with one, my search results on this have not gotten any clear results on how to do this with AFNetworking 2.x so I'd appreciate some pointers on where to go with this.

2 Answers 2

2

AFNetworking can automatically change paramter in NSDictionary to JSON.

change your manager's property requestSerializer to AFJSONRequestSerializer the default value is AFHTTPRequestSerializer

AFJSONRequestSerializer is a subclass of AFHTTPRequestSerializer that encodes parameters as JSON using NSJSONSerialization, setting the Content-Type of the encoded request to application/json.

Sign up to request clarification or add additional context in comments.

6 Comments

okay, but how do I send multiple dictionaries with only one request like that?
@Oz0234 merge your dictionaries into one
won't that just replace the values previously added for each key? Also the server fails to read the parameters correctly if I pass them inside a NSDictionary
@Oz0234 oh then you should use a array as paramter
the server still can't read the parameters of the request like that
|
0

For some reason just using the AFHTTPSessionManager Post function with the constructingBodyWithBlock parameter actually works for some reason. No idea why this is needed because there isn't actually anything in that block, if anyone could tell me why it would be nice.

    [self POST:@"?cmd=log"  parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        //For some reason the request won't work unless this block is included in the function as well, even though nothing is actually done in it
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        //Success code here
    } failure:^(NSURLSessionDataTask *task, NSError *error) {

    }];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.