Question

I have to use a webservice which takes HTTP Body format as the following

{
  "ScreenID" : "screenID1",
  "DeviceID" : "E7EF8DCE-CE8A-4F0C-BBC5-F080C29FEF29",
  "SessionStartTime" : "2014-03-27T06:50:15",
  "SessionEndTime" : "2014-03-27T06:50:15"
}

Now when i use NSMutableURLRequest instance it takes NSData instance as an argument

The code is as follows :-

NSMutableURLRequest *request = [[NSMutableRequest alloc] initWithURL:@"someurl"]
[request setHTTPMethod:@"POST"];

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

[request setValue:cipher forHTTPHeaderField:@"Cipher"];

NSData *jsonData;
if ([NSJSONSerialization isValidJSONObject:dict]) {
    NSError *error;
    jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
    if (!jsonData) {

        NSLog(@"json Data %@",error.description);

    } else {

        NSString *JSONString = [[NSString alloc] initWithBytes:[jsonData bytes] length:[jsonData length] encoding:NSUTF8StringEncoding];

        NSLog(@"JSON String %@",JSONString);//[JSONString dataUsingEncoding:NSUTF8StringEncoding]
        [request setHTTPBody:jsonData];

    }

}
else
{
    NSLog(@"This Data can't be serialized");

}
//NSLog(@"URL Request %@",[request allHTTPHeaderFields]);
NSLog(@"jsonDATA %@",jsonData);

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    if (connectionError) {
        //NSLog(@"Error %@",connectionError.description);
    }else
    {
        NSLog(@"%@",response);
        NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"data str %@",str);

    }
}];

The Contents of JSONstring are ==

{
  "ScreenID" : "screenID1",
  "DeviceID" : "E7EF8DCE-CE8A-4F0C-BBC5-F080C29FEF29",
  "SessionStartTime" : "2014-03-27T06:50:15",
  "SessionEndTime" : "2014-03-27T06:50:15"
}

The Contents of jsonDATA are ==

<7b0a2020 22536372 65656e49 4422203a 20224c61 6e64696e 67506167 65564322 2c0a2020 22446576 69636549 4422203a 20224537 45463844 43452d43 4538412d 34463043 2d424243 352d4630 38304332 39464546 3239222c 0a202022 53657373 696f6e53 74617274 54696d65 22203a20 22323031 342d3033 2d323754 30373a31 343a3336 222c0a20 20225365 7373696f 6e456e64 54696d65 22203a20 22323031 342d3033 2d323754 30373a31 343a3336 220a7d>

My Problem :-

since the format of HTTPBody in my webservice does not support the format generated by

[request setHTTPBody:jsonData];

however it would be ok if i could use

[request setHTTPBody:JSONstring];

but

the NSString argument can't be used with setHTTPBody method What should i do ? is there an alternative solution for this ?

Was it helpful?

Solution

Encoding the JSON string to data to set as the body data is the correct thing to do. It doesn't change the format or any of the string contents (your log of the data doesn't really mean anything in this form).

When the data gets to the server it will be used as a string, which is a JSON formatted string with a set of key/value pairs. This is expected behaviour.

If your server doesn't handle the JSON properly then you either have an issue with the headers that are set on the request or the keys/values that the JSON contains.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top