Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending HTTP POST request in iOS with JSON

So I'm currently using Facebook to login to my iOS app, and the server administrator recently added security authentication by requiring a Facebook access token along with making all connections go via https. Here's the code I've tried running, but I've been getting a server response error of 500 (internal server error) no matter what I do. Any ideas?

NSMutableURLRequest *request = [NSMutableURLRequest
                                requestWithURL:[NSURL URLWithString:@"https://XXXXXXXXXXXXXXXX/users.json"]];

NSDictionary *requestData = [[NSDictionary alloc] initWithObjectsAndKeys:
                     userID, @"facebook_id",
                     FBAccessToken, @"fb_token",
                     userName, @"name",
                     nil];
NSError *error;
NSData *postData = [NSJSONSerialization dataWithJSONObject:requestData options:0 error:&error];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
like image 232
Julian Coltea Avatar asked Mar 12 '13 02:03

Julian Coltea


People also ask

Can we send JSON via HTTP GET request?

To get JSON from a REST API endpoint, you must send an HTTP GET request and pass the "Accept: application/json" request header to the server, which will tell the server that the client expects JSON in response.

Can we send JSON object in post request in REST API?

To post JSON to a REST API endpoint, you must send an HTTP POST request to the REST API server and provide JSON data in the body of the POST message. You also need to specify the data type in the body of the POST message using the Content-Type: application/json request header.

Does POST use JSON?

To post JSON data to the server, we need to use the HTTP POST request method and set the correct MIME type for the body. The correct MIME type for JSON is application/json. In this POST JSON example, the Content-Type: application/json request header specifies the media type for the resource in the body.


2 Answers

Here is how you log the response body.

@property (strong, nonatomic) NSMutableData *responseData;

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    self.responseData = [NSMutableData data];
    …
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.responseData appendData:data];
    …
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"response data - %@", [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding]);
    …
}
like image 148
Jeffery Thomas Avatar answered Sep 30 '22 07:09

Jeffery Thomas


You also need to conform to the NSURLConnectionDelagate protocol.

like image 32
user2959060 Avatar answered Sep 30 '22 06:09

user2959060