I have an NSData object that represents a Facebook user provided by the IOS Facebook SDK. This object is JSON formatted and I am attempting to post it to a server application. Below is my Objective C code to execute the HTTP Post. The problem with this code is that the request body is empty on the server. Can someone spot what I’ve done wrong?
-(void)postUserDataToServer:(NSString *) user {
// Convert the JSON string to Data to be sent.
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://ec2-whatever.compute-1.amazonaws.com:3000/user"]];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%d", [user length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:user];
[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
}
well … user is not NSData! you gotta transform the string into data