How to send json data to Http request using NSURLRequest

I'm new to objective-c, and I'm starting to put a lot of effort into the request / response at a recent stage. I have a working example that can call a url (via http GET) and parse the returned json.

A working example of this is below.

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [responseData setLength:0]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [responseData appendData:data]; } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog([NSString stringWithFormat:@"Connection failed: %@", [error description]]); } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { [connection release]; //do something with the json that comes back ... (the fun part) } - (void)viewDidLoad { [self searchForStuff:@"iPhone"]; } -(void)searchForStuff:(NSString *)text { responseData = [[NSMutableData data] retain]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.whatever.com/json"]]; [[NSURLConnection alloc] initWithRequest:request delegate:self]; } 

My first question is: will this approach expand? Or this is not async (this means that I am blocking the user interface thread while the application is waiting for a response)

My second question is: how can I change the request part of this to do POST instead of GET? Is it easy to change HttpMethod like that?

 [request setHTTPMethod:@"POST"]; 

And finally, how to add a json dataset to this post as a simple string (for example)

 { "magic":{ "real":true }, "options":{ "happy":true, "joy":true, "joy2":true }, "key":"123" } 

Thank you in advance

+76
json objective-c iphone cocoa-touch nsurlrequest
Dec 16 '10 at 2:30
source share
8 answers

Here is what I am doing (note that the JSON going to my server must be a dictionary with one value (another dictionary) for key = question..ie {: question => {dictionary}}):

 NSArray *objects = [NSArray arrayWithObjects:[[NSUserDefaults standardUserDefaults]valueForKey:@"StoreNickName"], [[UIDevice currentDevice] uniqueIdentifier], [dict objectForKey:@"user_question"], nil]; NSArray *keys = [NSArray arrayWithObjects:@"nick_name", @"UDID", @"user_question", nil]; NSDictionary *questionDict = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; NSDictionary *jsonDict = [NSDictionary dictionaryWithObject:questionDict forKey:@"question"]; NSString *jsonRequest = [jsonDict JSONRepresentation]; NSLog(@"jsonRequest is %@", jsonRequest); NSURL *url = [NSURL URLWithString:@"https://xxxxxxx.com/questions"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; NSData *requestData = [jsonRequest dataUsingEncoding:NSUTF8StringEncoding]; [request setHTTPMethod:@"POST"]; [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"]; [request setHTTPBody: requestData]; NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self]; if (connection) { receivedData = [[NSMutableData data] retain]; } 

Then the received date is processed:

 NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSDictionary *jsonDict = [jsonString JSONValue]; NSDictionary *question = [jsonDict objectForKey:@"question"]; 

This is not 100% clear and will take some re-reading, but everything should be here to get you started. And from what I can say, it is asynchronous. My user interface is not blocked during these calls. Hope this helps.

+101
Dec 17 '10 at 1:11
source

I would suggest using ASIHTTPRequest

ASIHTTPRequest is an easy-to-use wrapper around the CFNetwork API, which makes some of the more tedious aspects of communicating with web servers easier. It is written in Objective-C and works on both Mac OS X and iPhone applications.

Suitable for performing the basic HTTP request protocol and interacting with REST-based Services (GET / POST / PUT / DELETE). The included ASIFormDataRequest Subclass makes it easy to send data and POST files using multipart / form-data.




Please note that the original author has discontinued this project. See Subsequent publication for reasons and alternatives: http://allseeing-i.com/%5Brequest_release%5D ;

I personally am a big fan of AFNetworking

+6
Dec 16 '10 at 2:33
source

I struggled with this for a while. Running PHP on the server. This code will send json and get json response from server

 NSURL *url = [NSURL URLWithString:@"http://example.co/index.php"]; NSMutableURLRequest *rq = [NSMutableURLRequest requestWithURL:url]; [rq setHTTPMethod:@"POST"]; NSString *post = [NSString stringWithFormat:@"command1=c1&command2=c2"]; NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding]; [rq setHTTPBody:postData]; [rq setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [NSURLConnection sendAsynchronousRequest:rq queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { if ([data length] > 0 && error == nil){ NSError *parseError = nil; NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError]; NSLog(@"Server Response (we want to see a 200 return code) %@",response); NSLog(@"dictionary %@",dictionary); } else if ([data length] == 0 && error == nil){ NSLog(@"no data returned"); //no data, but tried } else if (error != nil) { NSLog(@"there was a download error"); //couldn't download } }]; 
+6
Feb 11 '15 at 15:03
source

Most of you already know this, but I publish it, just expose, some of you are still struggling with JSON in iOS6 +.

In iOS6 and later, we have the NSJSONSerialization class , which is fast and independent of the inclusion of "external" libraries.

 NSDictionary *result = [NSJSONSerialization JSONObjectWithData:[resultStr dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil]; 

In this way, iOS6 and later can now parse JSON efficiently. Using SBJson is also a pre-ARC implementation and brings these issues with you if you are working in an ARC environment.

Hope this helps!

+3
Nov 12 '13 at 14:49
source

Here is a great article using Restkit

It explains the serialization of embedded data in JSON and data binding to an HTTP POST request.

+2
Aug 19 '13 at 16:34 on
source

Since my editing for Mike G, the answer to the code upgrade was rejected from 3 to 2 as

This change was intended to respond to the author of the message and does not make sense like editing. It should have been written as a comment or response.

I am rewriting my editing as a separate answer here. This edit removes the JSONRepresentation dependency with NSJSONSerialization as Rob's 15-hint comment.

  NSArray *objects = [NSArray arrayWithObjects:[[NSUserDefaults standardUserDefaults]valueForKey:@"StoreNickName"], [[UIDevice currentDevice] uniqueIdentifier], [dict objectForKey:@"user_question"], nil]; NSArray *keys = [NSArray arrayWithObjects:@"nick_name", @"UDID", @"user_question", nil]; NSDictionary *questionDict = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; NSDictionary *jsonDict = [NSDictionary dictionaryWithObject:questionDict forKey:@"question"]; NSLog(@"jsonRequest is %@", jsonRequest); NSURL *url = [NSURL URLWithString:@"https://xxxxxxx.com/questions"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; NSData *requestData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:nil]; //TODO handle error [request setHTTPMethod:@"POST"]; [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"]; [request setHTTPBody: requestData]; NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self]; if (connection) { receivedData = [[NSMutableData data] retain]; } 

Then the received date is processed:

 NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; NSDictionary *question = [jsonDict objectForKey:@"question"]; 
+2
Nov 03 '15 at 17:54
source

Here's an updated example that uses NSURLConnection + sendAsynchronousRequest: (10.7+, iOS 5+), the "Post" request remains the same as with the accepted answer, and omitted here for clarity:

 NSURL *apiURL = [NSURL URLWithString: [NSString stringWithFormat:@"http://www.myserver.com/api/api.php?request=%@", @"someRequest"]]; NSURLRequest *request = [NSURLRequest requestWithURL:apiURL]; // this is using GET, for POST examples see the other answers here on this page [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { if(data.length) { NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; if(responseString && responseString.length) { NSLog(@"%@", responseString); } } }]; 
0
Dec 9 '13 at 14:42
source

You can try this code to send json string

 NSData *jsonData = [NSJSONSerialization dataWithJSONObject:ARRAY_CONTAIN_JSON_STRING options:NSJSONWritin*emphasized text*gPrettyPrinted error:NULL]; NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; NSString *WS_test = [NSString stringWithFormat:@"www.test.com?xyz.php&param=%@",jsonString]; 
0
04 Oct '16 at 12:28
source



All Articles