iosobjective-ccodeigniter

NSURLRequest not posting values to a Codeigniter API


I am trying to use a Codeigniter based API to connect with iOS and using NSURLRequest. The API is in debugMode and for now it returns the same key value pair as JSON as the one that you are posting. I have tried posting the values to the link through postman and it works correctly, however when I post it through my iOS application, the JSON response is received but the array that should contain the post values is empty.

Here is the iOS Code snippet:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BASEURL,service]];

NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];

NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:request delegate:self];

NSString * params = @"authkey=waris";
NSData * postData = [params dataUsingEncoding:NSUTF8StringEncoding];

NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];

[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];;
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];

NSLog(@"Posting : '%@'  to %@",params,url);

[connection start];

This is the response when I post the same parameters through Postman (A RESTFUL Client for Chrome)

{
"status": "1",
"data": {
    "authkey": "warisali"
  }
}

However when I query the same API from the above iOS Code I am getting this:

{
  data = 0;
  status = 1;
}

Any help on the matter will be highly appreciated!


Solution

  • I ended up using the ASIHttpRequest + SBJson combo and that worked like Charm!

    After adding the ASIHttpRequest core classes and SBJson Classes to parse the JSON, I was able to achieve what I wanted !