0

I have successfully created a method which connects to my web service and POSTS a string and receives a string response. However, I am now trying to POST a JSON dictionary created using NSJSONSerialization. However, Xcode is giving me an error. I have tried to convert my initial code. Relevant lines below...

NSData* loginDataJSON = [NSJSONSerialization dataWithJSONObject:loginDataDictionary options:NSJSONWritingPrettyPrinted error:&error];
[request setValue:loginDataJSON forHTTPHeaderField:@"loginDataJSON"];
[request setHTTPBody:[loginDataJSON dataUsingEncoding:NSUTF8StringEncoding]];

The second line heres complains that I am using NSData where an NSString is required. The third line complains that loginDataJSON may not respond to dataUsingEnconding

I seem to be forcing an NSData object (because that what NSJSONSerialization gives me) where it cannot be used. Am I best trying to convert the JSON into a string to use with my existing request code, or should/can I change my request code to accept NSData as opposed to an NSString?

Ben Thompson
  • 4,743
  • 7
  • 35
  • 52

4 Answers4

1

Try this code:

let body = NSMutableDictionary()
body.setValue("myString" forKey: "stringType")
body.setValue(data?.base64EncodedString(options: .lineLength64Characters) forKey: "dataType")

In this way you can have both data and string in the dictionary. Here 'data?.base64EncodedString(options: .lineLength64Characters)' returns you a string with base64 encoding. So your dictionary contains only string and at server end you have to covert it back to data.

Hope it solves your issue.

zx485
  • 28,498
  • 28
  • 50
  • 59
paresh
  • 130
  • 9
0

Have you tried to use frameworks like JSONKit or RESTKit? RESTKit is specially used for webservice communication with JSON and internally uses JSONKit and one another parser. I think this will solve your problem and maybe some future one ;)

christian.vogel
  • 2,117
  • 18
  • 22
  • No, I haven't tried those yet. I am aware of them but I generally prefer using built in iOS methods where possible. But if no-one can point me in the right direction, I may have to turn to REST. – Ben Thompson Jun 05 '12 at 06:10
0

make a mutable request and

 [request setHTTPBodyWithString:myMessageJSON];

where method is as follows:

- (void)setHTTPBodyWithString:(NSString *)body {
    NSData *bodyData = [body dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    [self setValue:[NSString stringWithFormat:@"%d", [bodyData length]] forHTTPHeaderField:@"Content-Length"];
    [self setHTTPBody:bodyData];
}
Saad
  • 8,857
  • 2
  • 41
  • 51
0

You need to send the JSON as the body of the request, and maybe set the content type and content length:

NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:nil];
[request setValue:@"application/json" forHTTPHeaderField:@"content-type"];
[request setValue:[NSString stringWithFormat:@"%d", [loginDataJSON length]] forHTTPHeaderField:@"content-length"];
[request setHTTPBody:loginDataJSON];
DarthMike
  • 3,471
  • 1
  • 22
  • 18