NSURLRequest not posting values to a Codeigniter API NSURLRequest not posting values to a Codeigniter API codeigniter codeigniter

NSURLRequest not posting values to a Codeigniter API


I had same issue (not with CodeIgniter but with Ruby ...) Try something like this, solved my problem.

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BASEURL,service]];NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:request delegate:self];NSDictionary *paramDict = @{@"authkey": @"waris"};NSError *error = nil;NSData *postData = [NSJSONSerialization dataWithJSONObject:paramDict options:NSJSONWritingPrettyPrinted error:&error];if (error){    NSLog(@"error while creating data %@", error);    return;}    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];


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 !


The problem is that because of the way you're creating the connection, it will start immediately, before you've finished configuring the request. Thus, you're creating a mutable request, creating and starting the connection, then attempting to modify the request and then trying to start the request a second time.

You can fix that by changing the line that says:

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

To say:

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

Or, easier, just move the original instantiation of the NSURLConnection (without the startImmediately:NO) after you've finished configuring your request, and then eliminate the [connection start] line altogether.