1

I'm working with an API that sends back HTTP 406's for many different errors, along with a custom message (reason phrase). It may look something like:

406 Not Acceptable: User is already logged in 406 Not Acceptable: Missing password field 406 Not Acceptable: Node does not exist.

I can get the 406 status code and the standard "Not Acceptable" string using:

NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;

NSInteger statusCode = [HTTPResponse statusCode];

[NSHTTPURLResponse localizedStringForStatusCode:HTTPResponse.statusCode];

However I really require the reason phrase message to know how to handle the response. How can I get it, preferably using the standard iOS SDK?

Erich
  • 2,509
  • 2
  • 21
  • 24
  • This question is a duplicate, see: http://stackoverflow.com/questions/6372047/can-i-access-reason-phrase-from-the-http-status-line-in-nshttpurlresponse - which doesn't help us much, as there's no solution there either. – ecotax Oct 13 '15 at 12:55

3 Answers3

1

I really require the reason phrase message to know how to handle the response.

Then the API is broken. The reason phrase is a debugging aid only. It's not meant to inform client behaviour.

From RFC 2616 § 6.1.1:

The Status-Code is intended for use by automata and the Reason-Phrase is intended for the human user. The client is not required to examine or display the Reason- Phrase.

If there is information about the response that cannot be conveyed by the status code alone, the proper place for it is as a header or in the response body. The reason phrase is not a correct place to put information necessary for a client to use.

Jim
  • 72,985
  • 14
  • 101
  • 108
  • Yeah, but as an app developer I can't control someone else's broken API, I just have to deal with it. Which, in this case, involves parsing the 406 reason phrase. So, is it possible? – Erich Feb 18 '14 at 17:58
  • You could do it by implementing HTTP yourself, but that would be a massive undertaking. The correct way of solving this is to file a bug with whatever API this is, as it is fundamentally broken. – Jim Feb 18 '14 at 19:39
  • 1
    You could tell us what API it is that you are using, so that we can all avoid it. What this API does is beyond stupid. Of the three error reasons you showed us, the first shouldn't be an error, the second should be a 401, and the third should be a 402. If the website can't get this right, how can you trust it with anything? – gnasher729 Feb 18 '14 at 23:15
0

Status code 406 means that the server cannot respond with the accept-header specified in the request.

406 Not Acceptable The requested resource is only capable of generating content not acceptable according to the Accept headers sent in the request.

These error codes are not iOS-specific. If you want to display different messages based on different occurrence reasons, I suppose you should check it with your API/web server, and use conditions in your code to display your custom messages for each of them.

Neeku
  • 3,646
  • 8
  • 33
  • 43
0

Ultimately, you can get the reason phrase using the ASIHTTPRequest library.

http://allseeing-i.com/ASIHTTPRequest/

It was just as simple to use in my case as AFNetworking and NSURLSession.

Erich
  • 2,509
  • 2
  • 21
  • 24