You don't use the NSJSONSerialization
class for posting data. You use the NSURL*
API (NSURL
, NSURLConnection
, NSURLRequest
, etc.) instead.
NSURL *url = [NSURL URLWithString:@"http://example.com/foo.php"];
NSMutableURLRequest *rq = [NSMutableURLRequest requestWithURL:url];
[rq setHTTPMethod:@"POST"];
NSData *jsonData = [@"{ \"foo\": 1337 }" dataUsingEncoding:NSUTF8StringEncoding];
[rq setHTTPBody:jsonData];
[rq setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[rq setValue:[NSString stringWithFormat:@"%ld", (long)[jsonData length]] forHTTPHeaderField:@"Content-Length"];
[NSURLConnection sendAsynchronousRequest:rq completion:^(NSURLResponse *rsp, NSData *data, SError *err) {
NSLog(@"POST sent!");
}];