Reputation: 591
I am using AFNetworking and trying to figure out how to use PUT requests properly. This is what the API document has given me (just an easy example).
curl -X PUT -d '{"problem":[{"problem":"text"}]}'
"https://api.website.com/form?apiKey={apiKey}"
Params is
'{"problem":[{"problem":"text"}]}'
while urlStr is https://api.website.com/form?apiKey={apiKey}
If you need any more information please let me know.
Here is my put request being executed.
- (void) executePutRequest : (NSString *) url params : (NSString *) params
{
NSString *urlStr = [NSString stringWithFormat:@"%@?apikey=%@", url,apiKey];
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:
NSASCIIStringEncoding];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager PUT:urlStr parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
[operation setUserInfo:userinfo];
SBJsonParser *jsonparser = [SBJsonParser new];
id result = [jsonparser objectWithString:[operation responseString]];
if ( self.delegate != nil && [self.delegate respondsToSelector:finishSelector] ) {
[self.delegate performSelector:finishSelector withObject:result];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[operation setUserInfo:userinfo];
if ( self.delegate != nil && [self.delegate respondsToSelector:failSelector] ) {
[self.delegate performSelector:failSelector withObject:[operation error]];
}
}];
}
What am I doing wrong? I am getting a 400 error (bad request). This is what my log looks like.
Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo=0x7aee9a40 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7aeea400>
Upvotes: 0
Views: 402
Reputation: 3181
AFNetworking is meant to do some of the redundant work for you, including serializing and deserializing output to server and input from server respectively. Your code implies that you are trying to do it manually, too, which is incorrect.
You have params
set to be an NSString *
. Just pass it the NSDictionary *
it expects. e.g. NSDictionary *params = @{ @"problem": @[ @{ @"problem": @"text" }]};
, from your example.
If you enter the success block, then responseObject should be deserialized into the format you have configured. IIRC, the default response format is JSON -- but look it up. You don't need to further process the response -- it's been done for you. For example, if the JSON body is something like { "cat": { "says": "meow" } }
, then simply cast the object to an NSDictionary; e.g. NSDictionary *catDict = (NSDictionary *)responseObject;
Did you or your team write the web service? Do you know what the expected request and response format is?
If you have to further configure the request and response serializers, you can do so in your manager's constructor (so subclass the manager, and make your own). For e.g,
[self setRequestSerializer:[AFJSONRequestSerializer serializer]];
[self setResponseSerializer:[AFJSONResponseSerializer serializer]];
... would configure the client to send JSON to the server, and expect JSON back from the server.
Upvotes: 2