1

not really sure where to start here other then to dive into CF (I REALLY don't want to do that) but....

I have an NSURLConnection signing OAuth2 requests to an ASP.NET WebAPI Resource Server, this resource server returns JSON body AND statusCode 400. I have yet to find a way to parse the data from the response when it returns code 400.

Does anyone here have any ideas? I would prefer to keep using NSURLConnection as this is only an OAuth2 consumer class. My other code is using restkit, but I do not want the OAuth2 end to require said library.

Holyprin
  • 348
  • 1
  • 9
  • This I know, I know exactly what the response from the service is because I own both the resource and client ends, NSURLConnection returns 400, but then the "Body" is Bad Request instead of what it should be which is the JSON: { "error":"invalid_grant", "error_description":"Invalid Username/Password" } – Holyprin Oct 05 '12 at 15:09

3 Answers3

1

The process to parse data from a request which returns status 400 should be identical to that of a request returning status 200.

Simply note the status code in -connection:didReceiveResponse: and allow the request to continue; you will receive any additional data that the server sends in -connection:didReceiveData: as usual. Finally, you'll get a -connectionDidFinishLoading: call when all data has been received, and you can parse the JSON there.

jatoben
  • 3,079
  • 15
  • 12
  • This does not work, didReceiveData still returns "Bad Request" instead of the json, and I cannot parse the data out of just a connection object in connectionDidFinishLoading. Obviously this looks like I have to use Core Foundation and throw these junk NSURL objects away, you CAN receive a 400 statuscode and a body, it's not against standards. – Holyprin Oct 05 '12 at 16:27
  • How did you get a Bad Request in your `-connection:didReceiveData:` method? Just take the NSData object that's passed in and append it to any existing server data. It's fine to receive additional data even if the status code is 400 - see the [Github API](http://developer.github.com/v3/), for example. – jatoben Oct 05 '12 at 17:03
  • Sorry, I didn't see that the data contains "Bad Request" instead of the JSON that you expect. In this case, the server is sending you bad data. Check the Accept header as [mentioned below](http://stackoverflow.com/a/12750619/1720115); you can also use a proxy app like [Charles](http://www.charlesproxy.com) to watch the traffic between your app and the web service. – jatoben Oct 05 '12 at 17:12
1

Does your HTTP request Accept header specify "application/json"? If so, then this is probably an IIS bug and not iOS.

David
  • 31
  • 2
  • yes it does and no it is not, I'm able to parse the response from a windows app too using HttpResponseMessages. I ended up overriding the library I'm using that returns 400's and changed them to 401's which is what I believe they should be anyway for that particular error. – Holyprin Oct 05 '12 at 18:13
0

Interestingly enough, MVC4 ActionResult is broken in the RTM. Switching it over to Pure WebApi and fine tuning the response, I was able to finesse it into returning the proper data, it was likely serializing the json improperly which other languages weren't catching.

Holyprin
  • 348
  • 1
  • 9