I'm having a problem using AFNetworking with Parse.com. I'm trying to POST data to the db with no luck. I'm not too familiar with networking stuff and so I'm using this as a learning exercise.
I've gotten the GET command to work so I know that the db is set up correctly and working but POSTing is another matter. I'm using AFNetworking 2.2.0 to POST a simple test string to the parse backend.
Here is the code I have for the GET command which works:
-(void)getParseData{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:kSDFParseAPIApplicationId forHTTPHeaderField:@"X-Parse-Application-Id"];
[manager.requestSerializer setValue:kSDFParseAPIKey forHTTPHeaderField:@"X-Parse-REST-API-Key"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[manager GET:@"https://api.parse.com/1/classes/some_data/" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
and here is the code I have so far for the POSt command, which is't working:
-(void)postParseData{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:kSDFParseAPIApplicationId forHTTPHeaderField:@"X-Parse-Application-Id"];
[manager.requestSerializer setValue:kSDFParseAPIKey forHTTPHeaderField:@"X-Parse-REST-API-Key"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
manager.responseSerializer = [[AFJSONResponseSerializer serializer]init];
NSDictionary *params = @{@"test_string" : @"NameTest"};
[manager POST:@"https://api.parse.com/1/classes/warm_data/" parameters: params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"POST data JSON returned: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
I know there is a custom parse.com API for iOS for this exact purpose but I want to use AFNetworking to learn more and improve. I've tried several different things I'v found on the net and specifically from stack overflow but noting yielded a result. Here is the error message returned from the AFHTTPRequestOperation failure block:
2014-03-24 11:42:07.894 testApp[3222:60b] Error: Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: bad request (400)" UserInfo=0xfe0f400 {NSErrorFailingURLKey=https://api.parse.com/1/classes/some_data/, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x14780070> { URL: api.parse.com/1/classes/some_data/ } { status code: 400, headers {
"Access-Control-Allow-Origin" = "*";
"Access-Control-Request-Method" = "*";
"Cache-Control" = "no-cache";
Connection = "keep-alive";
"Content-Length" = 130;
"Content-Type" = "application/json; charset=utf-8";
Date = "Mon, 24 Mar 2014 11:42:07 GMT";
Server = "nginx/1.4.2";
"Set-Cookie" = "_parse_session=BAh7BkkiD3Nlc3Npb25faWQGOgZFRiIlYWQyNmFhMTA3ZTJkZjljYjA3MjZkZTA1MGQzNWIzNGE%3D--f7812e190b5812d8bba91aea2b12d51412bf84ce; domain=.parse.com; path=/; expires=Wed, 23-Apr-2014 11:42:07 GMT; secure; HttpOnly";
Status = "400 Bad Request";
"X-Runtime" = "0.049574";
"X-UA-Compatible" = "IE=Edge,chrome=1";
} }, NSLocalizedDescription=Request failed: bad request (400)}
Any and all help is appreciated. If I've forgotten any thing or omitted something needed to fix the problem just let me know and I'll add what I can.