Sending HTTP POST request in iOS using JSON

So, I'm currently using Facebook to log in to the iOS app, and the server administrator recently added security authentication, requiring a Facebook access token, and all connections go through https. Here's the code I tried to run, but I get a 500 server response error (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]; 
+7
source share
2 answers

This is how you register 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]); … } 
+3
source

You must also comply with the NSURLConnectionDelagate protocol.

+1
source

All Articles