0

I have a problem with my JSON-RPC query.

NSString *jsonString = @"{id:1,method:getHolidays,jsonrpc:2.0}";
NSString *requestString = [NSString stringWithFormat:@"%@",jsonString,nil];

NSData *requestData = [NSData dataWithBytes: [jsonString UTF8String] length: [requestString length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:@"https://neilo.webuntis.com/WebUntis/jsonrpc.do?school=htl_v%C3%B6cklabruck"]];

[request setHTTPMethod: @"POST"];
[request setValue:@"Content-type: application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:requestData];

NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];

NSLog(@"\n\n\noutput: %@",returnString);

As Result I get this output:

{"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"Parse error: Unexpected character ('i' (code 105)): was expecting double-quote to start field name\n at [Source: org.apache.catalina.connector.CoyoteInputStream@7a1efafd; line: 1, column: 3]"}}

The Result should be: ->message = not-authenticated<-

Does anyone have any idea what the problem is?

Anil_M
  • 10,893
  • 6
  • 47
  • 74
Darkdrummer
  • 301
  • 1
  • 2
  • 8
  • `NSDictionary *jsonDict = @{@"id":@1,@"method":@"getHolidays",@"jsonrpc":@"2.0"}; NSData *jsonData =[NSJSONSerialization dataWithJSONObject:jsonDict options:0 error:&error];`... `[request setHTTPBody:jsonData];` If you copy/paste the string JSON into a JSON Validator, you'll see it's not valid (missing quote surrounding strings). It should be `{"id": 1,"method": "getHolidays","jsonrpc": 2.0}`, which `NSJSONSerialization` would take care of it and simplifies since it's constructed from dictionary (easier to change parameter). – Larme Oct 27 '16 at 15:37
  • Yea Got it! NSString *jsonString = @"{\"id\":\"1\",\"method\":\"getHolidays\",\"jsonrpc\":\"2.0\"}"; – Darkdrummer Oct 27 '16 at 15:43
  • I'd recommend to use `NSJSONSerialization` rather than constructing your JSON String yourself. It's safer (there is an error parameter, if it's not valid, it's easier to modify, especially with mutable NSMutableArray/Dictionary, if there are key that can be added or not). Also, avoid `sendSynchronousRequest:returningResponse:error:`, prefers async ones (with `NSURLSession`). – Larme Oct 27 '16 at 15:46

1 Answers1

0

was expecting double-quote to start field name:

Your server is telling you that your JSON is invalid.

If you test {id:1,method:getHolidays,jsonrpc:2.0} on a JSON validator (http://jsonlint.com/ for instance), it would tell you so. As said, it's missing surrounding quotes for each strings (keys/values): {"id": 1,"method": "getHolidays","jsonrpc": 2.0} is valid.

So, as you tried @"{\"id\":\"1\",\"method\":\"getHolidays\",\"jsonrpc\":\"2.0‌​\"}" works.

I'd recommend to use NSJSONSerialization, sample not tested (may not compile):

NSDictionary *jsonDict = @{@"id":@1,
                           @"method":@"getHolidays",
                           @"jsonrpc":@"2.0"};
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:0 error:&error];
...
[request setHTTPBody:jsonData];

You don't have to escape the " (like \"), if there are conditions on your parameters, let's say if(someReason){doNotPutmethodKeyInJSON}, with a NSMutableDictionary, it's easier, and also easier to read (especially your new string with all the \" makes it harder to read where could be an issue) Also, in case of an invalid JSON, you can read the error.

I don't know where you put that code, but if it's main thread, that would block the UI, so avoid sendSynchronousRequest:returningResponse:error:, prefer async calls (with the help of NSURLSession)

Larme
  • 24,190
  • 6
  • 51
  • 81