How to Get NSURLResponse

nyanev picture nyanev · May 5, 2011 · Viewed 12.7k times · Source

I make http request to the web server with this:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://server.com"]]; 
[request setHTTPMethod: @"POST"];
[request setHTTPBody: myRequestData];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];

But how can I get the response from the server and parse it? Thanks a lot :)

Answer

Eimantas picture Eimantas · May 5, 2011

Since you're assigning response to returnData instance variable, convert it to string for starters just look what you get, the parsing may be done with either NSXMLParser or some JSON library depending on response format.

Here's how you'd convert the response to string:

NSString *responseBody = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];