Saturday, 10 August 2013

AFNetworking PUT request Error 400

AFNetworking PUT request Error 400

I am trying to make a PUT request using AFNetworking. There is a JSON on a
remote location, which I want to update with the request.
I have managed to serialize my object into JSON, and I have put it an
NSDictionary object:
NSDictionary *container = [NSDictionary dictionaryWithObject:[self
notifications] forKey:@"notifications"];
This way when I print out the container using NSLog, I get precisely the
JSON I want to send.
Then I try to use AFNetworking for my request:
AFHTTPClient *putClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL
URLWithString:@"http://creativemind.appspot.com"]];
[putClient setParameterEncoding:AFJSONParameterEncoding];
NSMutableURLRequest *putRequest = [putClient requestWithMethod:@"PUT"
path:@"/notifications"
parameters:container];
AFHTTPRequestOperation *putOperation = [[AFHTTPRequestOperation
alloc] initWithRequest:putRequest];
[putClient registerHTTPOperationClass:[AFHTTPRequestOperation
class]];
[putOperation
setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation,
id responseObject) {
NSLog(@"Response: %@", [[NSString alloc]
initWithData:responseObject encoding:NSUTF8StringEncoding]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
[putOperation start];
When I try this solution, I get the following error message:
2013-08-10 16:10:40.741 NotificationReader[1132:c07] Error: Error
Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in
(200-299), got 400" UserInfo=0x75c89e0
{NSLocalizedRecoverySuggestion={"cause":null,"class":"java.lang.NumberFormatException","localizedMessage":"null","message":"null"},
AFNetworkingOperationFailingURLRequestErrorKey=http://creativemind.appspot.com/notifications>,
NSErrorFailingURLKey=http://creativemind.appspot.com/notifications,
NSLocalizedDescription=Expected status code in (200-299), got 400,
AFNetworkingOperationFailingURLResponseErrorKey=}
I think it's important to note that when I try the same request with nil
parameters, I get te same error message. I also tried to perform a GET
request on the same JSON to check if I can reach it and it works
perfectly. I really have no idea what I could do. Any help would be
appreciated

No comments:

Post a Comment