1

I am getting this error using NSURLSession when I try to convert the NSData from the request to an NSDictionary object:

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x7f83f6ba11b0 {NSDebugDescription=Invalid value around character 0.}

I have logged the JSON that is being sent from my PHP backend, and it looks like this:

[{"comment":"refresh2","something":"Bob"},{"comment":"refresh","something":"Bob"},{"comment":"show","something":"Bob"},{"comment":"huh","something":"Bob"},{"comment":"erase replace","something":"Bob"},{"comment":"will it add this one?","something":"Bob"},{"comment":"a comment to add","something":"Bob"},{"comment":"upload","something":"Bob"},{"comment":"work","something":"Bob"},{"comment":"add","something":"Bob"},{"comment":"will it work?","something":"Bob"},{"comment":"did this add?","something":"Bob"},{"comment":"another comment","something":"Bob"},{"comment":"storycomment","something":"Bob"}]

I checked on jsonlint to make sure it is valid JSON...and it is.

Here is the code that produces the error:

NSError *jsonError;

NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data
                                            options:NSJSONReadingAllowFragments
                                              error:&jsonError];

I am quite baffled as I know that the JSON is valid. One thing I have done in the past to resolve this error was commenting out all of my echo and print/print_r statements in the php file that the request is being sent to. However, there are no echo or print statements in the php file...and I am still getting this error - any help would be greatly appreciated.

UPDATE

I followed the instructions found here to look for unwanted characters...although I don't know what to look for in the output...if someone could looks this over and tell me if their are any characters involved that would cause the error I am getting that would be awesome. I had to cut the output short - it all won't fit in this question.

2015-01-27 14:14:47.249 spiritswitch[813:15915] char=< hex=3c
2015-01-27 14:14:47.249 spiritswitch[813:15915] char=b hex=62
2015-01-27 14:14:47.250 spiritswitch[813:15915] char=r hex=72
2015-01-27 14:14:47.250 spiritswitch[813:15915] char=  hex=20
2015-01-27 14:14:47.251 spiritswitch[813:15915] char=/ hex=2f
2015-01-27 14:14:47.251 spiritswitch[813:15915] char=> hex=3e
2015-01-27 14:14:47.251 spiritswitch[813:15915] char=
 hex=a
2015-01-27 14:14:47.252 spiritswitch[813:15915] char=< hex=3c
2015-01-27 14:14:47.252 spiritswitch[813:15915] char=f hex=66
2015-01-27 14:14:47.253 spiritswitch[813:15915] char=o hex=6f
2015-01-27 14:14:47.253 spiritswitch[813:15915] char=n hex=6e
2015-01-27 14:14:47.253 spiritswitch[813:15915] char=t hex=74
2015-01-27 14:14:47.254 spiritswitch[813:15915] char=  hex=20
2015-01-27 14:14:47.254 spiritswitch[813:15915] char=s hex=73
2015-01-27 14:14:47.254 spiritswitch[813:15915] char=i hex=69
2015-01-27 14:14:47.254 spiritswitch[813:15915] char=z hex=7a
2015-01-27 14:14:47.254 spiritswitch[813:15915] char=e hex=65
2015-01-27 14:14:47.255 spiritswitch[813:15915] char== hex=3d
2015-01-27 14:14:47.255 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.255 spiritswitch[813:15915] char=1 hex=31
2015-01-27 14:14:47.255 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.256 spiritswitch[813:15915] char=> hex=3e
2015-01-27 14:14:47.256 spiritswitch[813:15915] char=< hex=3c
2015-01-27 14:14:47.256 spiritswitch[813:15915] char=t hex=74
2015-01-27 14:14:47.257 spiritswitch[813:15915] char=a hex=61
2015-01-27 14:14:47.257 spiritswitch[813:15915] char=b hex=62
2015-01-27 14:14:47.257 spiritswitch[813:15915] char=l hex=6c
2015-01-27 14:14:47.257 spiritswitch[813:15915] char=e hex=65
2015-01-27 14:14:47.258 spiritswitch[813:15915] char=  hex=20
2015-01-27 14:14:47.258 spiritswitch[813:15915] char=c hex=63
2015-01-27 14:14:47.258 spiritswitch[813:15915] char=l hex=6c
2015-01-27 14:14:47.259 spiritswitch[813:15915] char=a hex=61
2015-01-27 14:14:47.259 spiritswitch[813:15915] char=s hex=73
2015-01-27 14:14:47.259 spiritswitch[813:15915] char=s hex=73
2015-01-27 14:14:47.259 spiritswitch[813:15915] char== hex=3d
2015-01-27 14:14:47.260 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.260 spiritswitch[813:15915] char=x hex=78
2015-01-27 14:14:47.260 spiritswitch[813:15915] char=d hex=64
2015-01-27 14:14:47.260 spiritswitch[813:15915] char=e hex=65
2015-01-27 14:14:47.260 spiritswitch[813:15915] char=b hex=62
2015-01-27 14:14:47.261 spiritswitch[813:15915] char=u hex=75
2015-01-27 14:14:47.261 spiritswitch[813:15915] char=g hex=67
2015-01-27 14:14:47.261 spiritswitch[813:15915] char=- hex=2d
2015-01-27 14:14:47.261 spiritswitch[813:15915] char=e hex=65
2015-01-27 14:14:47.262 spiritswitch[813:15915] char=r hex=72
2015-01-27 14:14:47.262 spiritswitch[813:15915] char=r hex=72
2015-01-27 14:14:47.262 spiritswitch[813:15915] char=o hex=6f
2015-01-27 14:14:47.262 spiritswitch[813:15915] char=r hex=72
2015-01-27 14:14:47.263 spiritswitch[813:15915] char=  hex=20
2015-01-27 14:14:47.263 spiritswitch[813:15915] char=x hex=78
2015-01-27 14:14:47.263 spiritswitch[813:15915] char=e hex=65
2015-01-27 14:14:47.263 spiritswitch[813:15915] char=- hex=2d
2015-01-27 14:14:47.264 spiritswitch[813:15915] char=n hex=6e
2015-01-27 14:14:47.264 spiritswitch[813:15915] char=o hex=6f
2015-01-27 14:14:47.264 spiritswitch[813:15915] char=t hex=74
2015-01-27 14:14:47.264 spiritswitch[813:15915] char=i hex=69
2015-01-27 14:14:47.264 spiritswitch[813:15915] char=c hex=63
2015-01-27 14:14:47.264 spiritswitch[813:15915] char=e hex=65
2015-01-27 14:14:47.264 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.265 spiritswitch[813:15915] char=  hex=20
2015-01-27 14:14:47.265 spiritswitch[813:15915] char=d hex=64
2015-01-27 14:14:47.265 spiritswitch[813:15915] char=i hex=69
2015-01-27 14:14:47.265 spiritswitch[813:15915] char=r hex=72
2015-01-27 14:14:47.265 spiritswitch[813:15915] char== hex=3d
2015-01-27 14:14:47.266 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.266 spiritswitch[813:15915] char=l hex=6c
2015-01-27 14:14:47.266 spiritswitch[813:15915] char=t hex=74
2015-01-27 14:14:47.266 spiritswitch[813:15915] char=r hex=72
2015-01-27 14:14:47.266 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.266 spiritswitch[813:15915] char=  hex=20
2015-01-27 14:14:47.266 spiritswitch[813:15915] char=b hex=62
2015-01-27 14:14:47.267 spiritswitch[813:15915] char=o hex=6f
2015-01-27 14:14:47.267 spiritswitch[813:15915] char=r hex=72
2015-01-27 14:14:47.267 spiritswitch[813:15915] char=d hex=64
2015-01-27 14:14:47.267 spiritswitch[813:15915] char=e hex=65
2015-01-27 14:14:47.268 spiritswitch[813:15915] char=r hex=72
2015-01-27 14:14:47.268 spiritswitch[813:15915] char== hex=3d
2015-01-27 14:14:47.268 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.268 spiritswitch[813:15915] char=1 hex=31
2015-01-27 14:14:47.269 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.269 spiritswitch[813:15915] char=  hex=20
2015-01-27 14:14:47.269 spiritswitch[813:15915] char=c hex=63
2015-01-27 14:14:47.270 spiritswitch[813:15915] char=e hex=65
2015-01-27 14:14:47.270 spiritswitch[813:15915] char=l hex=6c
2015-01-27 14:14:47.270 spiritswitch[813:15915] char=l hex=6c
2015-01-27 14:14:47.271 spiritswitch[813:15915] char=s hex=73
2015-01-27 14:14:47.271 spiritswitch[813:15915] char=p hex=70
2015-01-27 14:14:47.271 spiritswitch[813:15915] char=a hex=61
2015-01-27 14:14:47.272 spiritswitch[813:15915] char=c hex=63
2015-01-27 14:14:47.273 spiritswitch[813:15915] char=i hex=69
2015-01-27 14:14:47.273 spiritswitch[813:15915] char=n hex=6e
2015-01-27 14:14:47.273 spiritswitch[813:15915] char=g hex=67
2015-01-27 14:14:47.274 spiritswitch[813:15915] char== hex=3d
2015-01-27 14:14:47.274 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.274 spiritswitch[813:15915] char=0 hex=30
2015-01-27 14:14:47.341 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.342 spiritswitch[813:15915] char=  hex=20
2015-01-27 14:14:47.342 spiritswitch[813:15915] char=c hex=63
2015-01-27 14:14:47.342 spiritswitch[813:15915] char=e hex=65
2015-01-27 14:14:47.343 spiritswitch[813:15915] char=l hex=6c
2015-01-27 14:14:47.343 spiritswitch[813:15915] char=l hex=6c
2015-01-27 14:14:47.343 spiritswitch[813:15915] char=p hex=70
2015-01-27 14:14:47.343 spiritswitch[813:15915] char=a hex=61
2015-01-27 14:14:47.344 spiritswitch[813:15915] char=d hex=64
2015-01-27 14:14:47.344 spiritswitch[813:15915] char=d hex=64
2015-01-27 14:14:47.344 spiritswitch[813:15915] char=i hex=69
2015-01-27 14:14:47.345 spiritswitch[813:15915] char=n hex=6e
2015-01-27 14:14:47.345 spiritswitch[813:15915] char=g hex=67
2015-01-27 14:14:47.345 spiritswitch[813:15915] char== hex=3d
2015-01-27 14:14:47.346 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.346 spiritswitch[813:15915] char=1 hex=31
2015-01-27 14:14:47.346 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.347 spiritswitch[813:15915] char=> hex=3e
2015-01-27 14:14:47.347 spiritswitch[813:15915] char=
 hex=a

UPDATE

Here is the PHP code that handles the response when I make a request - I have been told that I should change the Content-Type to application/json but I have tried that and it didn't help - I will try again - but let me know if you see something wrong with this code:

function sendResponse($status=null, $body = '', $content_type = 'text/html')
{
    $status_header = 'HTTP/1.1 ' . $status . ' ' . getStatusCodeMessage($status);
    header($status_header);
    header('Content-type: ' . $content_type);
    print $body;
}
Community
  • 1
  • 1
ewizard
  • 2,801
  • 4
  • 52
  • 110
  • 1
    The JSON that you're sending from your server is an array, but you assign it to an `NSDictionary`. I'm not 100% sure that's your problem, but you should probably be more careful. – Ian MacDonald Jan 27 '15 at 19:15
  • @IanMacDonald i updated my question...I will try to store it in an array instead of a dictionary...but the dictionary should work too I think. – ewizard Jan 27 '15 at 19:22
  • @IanMacdonald - i tried `NSError *jsonError = nil; NSArray *jsonArray = (NSArray *)[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&jsonError];` and i get the same error. – ewizard Jan 27 '15 at 19:29
  • Your output is HTML. It looks like you're not receiving the response you think you're receiving. You should probably not output each character on its own line; putting them together into a string makes it way easier to read... `
    `
    – Ian MacDonald Jan 27 '15 at 19:32
  • @IanMacDonald could you elaborate please? one thing i am noticing...I am using httprequester in firefox to test...and it says that I am not getting back a response...but if I use curl from the terminal - I get a response...why? – ewizard Jan 27 '15 at 19:35
  • @IanMacDonald with httprequester...i get a successful `202` response...but no json data is output... – ewizard Jan 27 '15 at 19:36
  • @IanMacDonald - if you are talking about the "byte analysis" I was just following instructions from another stack overflow error...but I think you are on to something with noticing the output is html....don't i want it to be json? – ewizard Jan 27 '15 at 19:37
  • Your server is *not returning JSON* to you. It is returning *HTML*. I wrote out the response that it's giving you. Your logs show one character per line (example: `2015-01-27 14:14:47.249 spiritswitch[813:15915] char=< hex=3c` the `<` in that line). Putting them together reveals a pretty typical HTML snippet; not JSON. – Ian MacDonald Jan 27 '15 at 19:38
  • You need to fix your server or set your accepts type to JSON or something. It's hard to say what's wrong here as it's clearly your transport and nothing to do with any of the code you've posted. – Ian MacDonald Jan 27 '15 at 19:38
  • @IanMacDonald i have actually tried changing the response headers to "application/json" but that didnt work...write now they are "text/html" as that is what has been working for all my other requests...i will paste the code from php in charge of the response...one sec... – ewizard Jan 27 '15 at 19:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69689/discussion-between-ewizard-and-ian-macdonald). – ewizard Jan 27 '15 at 19:43
  • @IanMacDonald - hoping you can chat so i can fix this - changing the `content-type` in the php response function did nothing. – ewizard Jan 27 '15 at 19:46

1 Answers1

4

This might sound stupid - but are you sure you're asking for the correct URL? Please see my other post which I encountered the same problem. https://stackoverflow.com/a/28393273/557362

First start with the URL response, and see what's really happening. You can NSLog [NSString stringWithUTF8String:[data bytes]] to get the nice version of data, but don't forget to look at the HTTP response. Log [response description].

Community
  • 1
  • 1
GilesDMiddleton
  • 2,279
  • 22
  • 31
  • i eventually solved the problem by switching to afnetworking and interpreting the json object as an array instead of an nsdictionary - i realize now maybe all I needed to do was interpret the data as an array instead of an nsdictionary to solve the problem while still using nsurlsession... – ewizard Feb 08 '15 at 15:31