Within iOS I am building an http request as follows:
NSURL* url = [ NSURL URLWithString:[ NSString stringWithFormat: @"%@%@", [ HPSWebService getBaseURL ], @"Sync/PushObject"] ];
// 'request' is defined within the base class and is set here to the class-specific server url
request = [[NSMutableURLRequest alloc] initWithURL:url];
NSString* jsonRequest = [NSString stringWithFormat: @"{\"collection\":\"responses\",\"id\":\"%@\",\"objectjson\":%@}",response.id,response.json];
NSLog(@"HPSPushResponsesWebService jsonRequest = %@",jsonRequest);
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:self.contentEncoding forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];
The NSLog output is as follows:
2012-10-23 15:45:51.543 HPS[12523:707] HPSPushResponsesWebService jsonRequest = {"collection":"responses","id":"ZBZiaa43QciMSlMCp0Vtyw==","objectjson":{"_id":"{ \"$binary\" : \"ZBZiaa43QciMSlMCp0Vtyw==\", \"$type\" : \"03\" }","_user":"Eddie Freshman","_lastmodifieddatelocalutc":"2012-10-23 14:45:39","Fav Car":["Other:Meëzoom"],"_formid":"{ \"$binary\" : \"OCDI9VNGStmN8GbOLSevtA==\", \"$type\" : \"03\" }","Follow-up Required?":[],"_contactid":"{ \"$binary\" : \"+3144XhxQzCNzWKfqXCHyg==\", \"$type\" : \"03\" }"}}
It all works fine until the JSON data contains a special character (such as an umlaut). When this happens then the content length is actually 1 char less than it should be. In the example the umlat (e) char has been converted into ë (near the Fav Car string)
How should I code this so the content length is correctly set within the http request?
Thanks