Search code examples
iosobjective-cjsonxcodensmutableurlrequest

NSMutableURLRequest with parameter - cannot parse response


I am trying to create a connection to a URL that prints out JSON code that is then parsed and processed. My URL has a parameter called bypass. The URL looks like this:

http://www.getchanged.net/json.asp?bypass=MIGCBgkrBgEEAYI3WAOxxx

I did not manage to solve this issue as it always says Connection failed with error: cannot parse response

NSURL *aUrl = [NSURL URLWithString:@"http://www.getchanged.net/json.asp"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

[request setHTTPMethod:@"GET"];
NSString *postString = @"bypass=MIGCBgkrBgEEAYI3WAOxxx";
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

Error:

2014-10-29 21:43:39.529 TestMap[57987:1931535] Connection failed with error: cannot parse response

What is wrong here?

Please note: I replaced the bypass hash a bit because it contains sensible data. The website works and is printing out JSON output.


Solution

  • Apart from the issue well addressed by Rob, the JSON return from the listed website doesn't validate:

    I've tried several validators with the URL and bypass hash (you posted in comments) and the JSON is invalid. Typical error returned:

    JSON Content: Invalid JSON at line 26, column 84
      Parse error on line 26:
      ...rtragen", "adresse":"fairtragen - GmbH 
      -----------------------^
      Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'
    

    As explained at json.org any string value can contain

    any UNICODE character except " or \ or control character

    So a string value has as structure like this:

    enter image description here

    Note the escape format required for newlines and carriage returns.

    Taking that into account, and looking at the raw output captured in Charles you can see that line 26, column 84 contains x0d followed by x0a (i.e. an old windows style CRLF) in the middle of a value.

    The server should be escaping these values before sending them out.

    By the way, the JSON has lots of spaces (x20) and CRLF through out, outside of the array, objects and values, that while not affecting the JSON, are unnecessary and do bulk up the data transfer.