Can I access the Phrase of Reason from the HTTP status bar in NSHTTPURLResponse

RFC 2616 indicating HTTP says - in section 6.1.1 - this part of the status bar is a three-digit digital status code and the text "reason phrase".

I am creating an iPhone application using NSURLConnection to access data via HTTP. I can get the HTTP status code without a problem, but how can I access the “sentence phrase”?

Here is my connection: didReceiveResponse: method

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; httpStatusCode = [httpResponse statusCode]; // Reason Phrase ?? } 

To be specific, I do NOT mean "an explanation of what xxx means." I can find that in the RFC and they are static. I mean the text created by the server in the status bar. An example of such a status bar would be:

 HTTP/1.1 412 ClientAppVersion: 0.10 < 0.11 

and the phrase of the mind will be "ClientAppVersion: 0.10 <0.11" here.

This example also gives a hint of what I'm trying to do. I am creating a REST-like API, and therefore I have to use HTTP status codes to indicate errors. But HTTP status codes were invented for HTTP, and not for my application, so I'm trying to cram extra information into a sentence of reason.

+6
source share
2 answers

Following the previous comments (this is not exactly the answer you are looking for).

The HTTP specification (RFC 2616) states the status codes and the argument phrase :

The status code is designed to use machines and mind phrases designed for the user. the client is not required to check or display the phrase expression.

It is clear from the text that you should not expect to read the reason from the HTTP client. In fact, this is often a localized version that can be presented, if at all (not necessarily the one sent by the server).

The goal is to have standards and specifications such as HTTP in order to be able to expect various compatible implementations (like your server and iOS libraries) to interact. You should expect problems if you bend the specifications. In particular, do not be surprised if the library you want to use does not give you access to the reason phrase.

I'm not quite sure how to interpret your comment (“I bend HTTP to fit the idea of ​​REST.”) I can assure you that REST can be implemented using HTTP without such bends. I'm not sure where you got this idea of ​​HTTP bending to fit the idea of ​​REST ...

If you want to implement something to explain the reason for the REST error, the reason should be indicated in the body of the response message (or even, perhaps, in the user header), and not in the reason phrase. Even if it is a textual answer, it is better than a phrase of reason. For instance:

Instead:

 HTTP/1.1 412 ClientAppVersion: 0.10 < 0.11 

using:

 HTTP/1.1 412 Precondition Failed Content-Type: text/plain ClientAppVersion: 0.10 < 0.11 

or perhaps:

 HTTP/1.1 412 Precondition Failed Content-Type: text/plain X-My-Error: ClientAppVersion: 0.10 < 0.11 

Please note that you must return the body of the message in any case (except 204). The 412 status code is also most definitely associated with header-based preconditions (which you can use):

A prerequisite given in one or more fields of the request-request field to false when it has been tested on the server. This response code allows the client to place preconditions on the current meta-information of the resource (header field data) and thus prevent the requested method from being applied to a resource other than the one intended.

+4
source

Class method localizedStringForStatusCode: in NSHTTPURLResponse you will get a localized phrase for the status code received in the response.

0
source

Source: https://habr.com/ru/post/890676/


All Articles