I’ve searched and can’t find this already on here:
I’m trying to send an NSData object to a web service. The NSData object is a JSON object using iOS 5’s built in JSON.
I need to do an async send, and then check the response back from the server.
I do NOT want to use any 3rd party libraries.
Here is what I’ve got already that doesn’t work:
NSDictionary * initialLogAsJSON = [NSDictionary dictionaryWithObjects:items forKeys:keys];
NSError * error;
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:initialLogAsJSON options:NSJSONWritingPrettyPrinted error:&error];
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:kURLSendTestStartedDataToTenOneServer ]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"appliction/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: jsonData];
[NSURLConnection sendAsynchronousRequest:request
queue:self.queue
completionHandler:^( NSURLResponse * response, NSData * data, NSError * error )
{
NSLog( @"If finished with reponse = %@\nData: %@\nError %@", response, data, error );
// do something useful
}
];
Actually the problem was easier that than..
It was down to the URL being nil – because of some funny escape characters in the source strings.
Here’s my final (working) implementation: