2

I am sending two JSON dictionaries to an url. This is working with NSURLRequest but not with ASIFormDataRequest. When sending the same request with ASIFormDataRequest it shows the following error -

{"Response":"Error","Code":"400","Detail":"Unable to parse JSON in form.request"}

Below is the code I am using with ASIFormDataRequest -

NSString *request=[NSString stringWithFormat:@"REQUEST={\"request\":{\"api_username\":\"%@\", \"api_password\":\"%@\",\"method\":\"%@\"},",@"iphone",@"xlq229320#",@"getUser"];
NSString *methodParameter=[NSString stringWithFormat:@"\"methodparam\":{\"email\":\"%@\",\"password\":\"%@\"}}",@"ganesh@gmail.com",@"ganesh"];
NSString *finalRequest=[request stringByAppendingString:methodParameter];

NSMutableData* requestData = [NSMutableData dataWithBytes: [finalRequest UTF8String] length: [finalRequest length]];

ASIFormDataRequest *asiRequest = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:urlForClinic]];

[asiRequest setTimeOutSeconds:60.0];
[asiRequest setPostBody:requestData];
[asiRequest setDelegate:self];
[asiRequest setRequestMethod:@"POST"];
[asiRequest setDidFailSelector:@selector(requestFailed:)];
[asiRequest setDidFinishSelector:@selector(requestFinished:)];
[asiRequest startAsynchronous];

I could not understand where I am doing wrong. Please let me know if anybody has any clue about this. Thanks much.

I even tried it with AFNetworking, but it too didnt worked. Here is the code -

NSDictionary *dict1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"iphone",@"api_username",@"xlq229320#",@"api_password",@"getUser",@"method", nil];
NSDictionary *dict2 = [[NSDictionary alloc]initWithObjectsAndKeys:@"ganesh@gmail.com",@"email",@"ganesh",@"password",nil];
NSMutableDictionary *req = [[NSMutableDictionary alloc]initWithObjectsAndKeys:dict1,@"request",dict2,@"methodparam",nil];

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:req options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];    
NSData *requestData = [NSData dataWithBytes:[jsonString UTF8String] length:[jsonString length]];

NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:urlForClinic] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:requestData];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
  NSLog(@"Public Timeline: %@ %@", JSON, response);
} failure:nil];

[operation start];
Namit Gupta
  • 796
  • 9
  • 20
  • Why are you not using AFNetworking? – Rajan Balana Mar 20 '13 at 09:01
  • @rajan-balan The reason I am sticking to ASIHTTP is because it has the ability to work when the app is in background as well, using the "setShouldContinueWhenAppEntersBackground" property. I tried with AFNetworking as well, but it too didnt worked. I have updated my question with the code. Please check. – Namit Gupta Mar 20 '13 at 09:22

1 Answers1

0

Format the data you want to send into Key/Value pairs in an NSDictionary then use NSJSONSerialization to create the NSData object you need to send. It'll be more compatible.

NSDictionary *jsonDict = @{key:value,key:value};
NSData *requestData = [NSJSONSerialisation dataWithJSONObject:jsonDict options:NSJSONWritingPrettyPrinted error:error];
Cocoadelica
  • 3,006
  • 27
  • 30
  • Thanks @mightyleader, I tried this way as well. But it didnt worked. – Namit Gupta Mar 20 '13 at 09:25
  • Ah sorry to hear that. Is there a reason that you're using ASIHTTP stuff? The newer version of NSURLConnection with block methods work really well for asynchronous connections. Duh just read your answer to someone elses comment. Still might be worth checking out the newer ways of using the standard methods. – Cocoadelica Mar 20 '13 at 09:30
  • Got the solution. The problem was because I didnt set any request headers to the request. Adding the following lines to the request solved the problem - NSMutableDictionary *requestheaders =[[NSMutableDictionary alloc]init]; [requestheaders setValue:@"application/x-www-form-urlencoded; charset=utf-8" forKey:@"Content-Type"]; [asiRequest setRequestHeaders:requestheaders]; – Namit Gupta Mar 20 '13 at 13:12