Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking Overload a Post Parameter

I am migrating from ASIHTTPRequest to AFNetworking and have run into an issue.

I am trying to hit an API with a request that overloads the post parameter. I was previously using ASIFormDataRequest for this and used this code to update 3 ids at the same time.

// ASIHTTPRequestCode
[request addPostValue:@"value1" forKey:@"id"];
[request addPostValue:@"value2" forKey:@"id"];
[request addPostValue:@"value3" forKey:@"id"];

Since AFNetworking uses an NSDictionary to store key value pairs, it doesn't seem straight forward how to do this. Any ideas?

like image 876
lavoy Avatar asked Mar 14 '26 23:03

lavoy


1 Answers

I can't immediately see a direct way to do this with AFNetworking, but it is possible to do.

If you look at the code for AFHTTPClient requestWithMethod, you'll see this line, which is the one that sets up the request body to contain the parameters:

 [request setHTTPBody:[AFQueryStringFromParametersWithEncoding(parameters, self.stringEncoding) dataUsingEncoding:self.stringEncoding]];

Basically you could pass an empty dictionary to requestWithMethod for the parameters, then it returns, call request setHTTPBody yourself, making up the query string yourself in a similar way to the way AFQueryStringFromParametersWithEncoding does it.

like image 185
JosephH Avatar answered Mar 16 '26 13:03

JosephH



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!