How do HTTP response status codes work?

Asked

Viewed 767 times

5

I’m redirecting a user to the root route in case they don’t pass a condition and the slim framework asks for an http status for redirection.

With this I did not understand exactly how this process works, I noticed that the 301 status indicates me a moved state and updates future requests for the new page.

How HTTP status works and what would be the requests cited above

Excerpt from my code:

$app->post('/contact', function(Request $request, Response $response) {
    if (condição) {
        //code
    } else {
        return $response->withRedirect('/', 301);
    }
}
  • 5

    It’s just an indication for the customer who is consuming your application. Returning a 301 means that you have changed the URL, that the resource that it is accessing is accessible in another URL; so it can, in the next requests, already make the request in the correct URL. The 302, which is the one that best suits the situation, but not perfectly, indicates to the client that the resource he is accessing is temporarily in another URL and that he should continue ordering in the same future (I’m on mobile, I can not make a complete answer).

  • Thanks, it helped a lot.

2 answers

7


To understand what the status code is, you first need to know what HTTP is:

An application usually has two parts, one that runs on the client side (browser, native app, etc.) and one that runs on the server side. In the case of the web, the first can only be HTML, CSS and JS, while the other can be JS, Python, C#, Java among several others, in your case, is PHP

HTTP is briefly the communication format between your PHP (or anything else) and your client. It consists of a request and response pair, meaning it has a structure that is used for your client to ask for something for the server (called a request, or request, in English) and is followed by another structure that serves for the server to deliver what has been requested (response call, or sponse, in English). Both are similar:

Requisition

POST /contact_form.php HTTP/1.1
Host: developer.mozilla.org
Content-Length: 64
Content-Type: application/x-www-form-urlencoded

name=Joe%20User&request=Send%20me%20one%20of%20your%20catalogue

Answer

HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 55743
Content-Language: en-US

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>A simple webpage</title>
</head>
<body>
  <h1>Simple HTML5 webpage</h1>
  <p>Hello, world!</p>
</body>
</html>

The first line of requisition informs, in sequence:

The first line of reply informs, in sequence:

  • HTTP version, it’s the same thing as the request

  • Answer code, I’ll explain later, after all is main doubt

  • Code description, basically is a word or expression that accompanies the code, each code has its description

From then on (sgunda line on) is equal in both requests and responses

  • the headings, or headers, in English. It has the format Nome-Do-Cabeçalho: Valor, are useful information that can be used by the receiver (server or client) to decide whether and how to treat that data, for example type of content (Content-Type), cache (Cache-Control, Expires, ...), language of content (Accept-Language and Content-Language), authentication (Authorization), among several others

  • A blank line, it splits the headers of the next and last part...

  • The body, or body, in English, it is the content itself, can be in several formats (usually specified in the header). In the request it usually contains data to read (SELECT * FROM ...) or write (INSERT INTO ... or UPDATE ... SET ... or DELETE FROM ...) something on the server side. In the answer contains the data that was requested, it can be a JSON, XML, HTML, PDF, CSV or anything else

You can look at the wiki tag or search for questions/answers with


Now that you know, at least I hope you know, what is HTTP, let’s ask, "How HTTP response status codes work?"

Well, if you requested something for your server we can agree that the answer may vary, for example, if someone is trying to change the user password, but is not logged in, the answer must be an error, since if the person is properly authenticated, then there must be a message of success. The status serves for this, say what is the type of answer. It is divided into ranges (I’m not sure how I could translate, maybe classes or divisions):

  • 1xx: all status starting with 1 are of the informative type (I believe it is very little used, if I ever used really do not remember)

  • 2xx: all status starting with 2 are of type success, the request had no error and the server was able to process

  • 3xx: all status starting with 3 is redirect like in 2xx, everything ok, but content with the answer is elsewhere

  • 4xx: all status starting with 4 are of the client-side error type, the request has some problem and the server could not process

  • 5xx: all status starting with 5 are of the server-side error type, the request is correct, but the server could not process due to some problem

To understand how the crease 1xx, from a look in that question, requests and responses work a little differently

For 2xx I’ll give two:

  • 200 Ok is the typical response when everything is right, correct request and server managed to respond

  • 201 Created is the answer not so used, it means that everything is ok, as in the previous one, but also that the resource (anything) was created

The 3xx, which is the focus of your question, I will put all, however copied from MDN:

  • 300 Multiple Choice: the request has more than one possible answer. User-agent or user should choose one of them. There is no standard way to choose one of the answers.

  • 301 Moved Permanently: this response code means that the required resource URI has changed. Probably, the new URI will be specified in the response.

  • 302 Found: this response code means that the URI of the requested resource has been temporarily changed. Further changes to the URI may be made in the future. Therefore, the same URI should be used by the client in future requests.

  • 303 See Other: the server sends this response to instruct the client to search for the requested resource in another URI with a GET request.

  • 304 Not Modified: this answer is used for cache questions. It tells the client that the answer has not been modified. Therefore, the client can use the same cached version of the response.

  • 305 Use Proxy: was defined in an earlier version of the HTTP specification to indicate that a response must be accessed by a proxy. Depreciated for security reasons in respect of the bandwidth configuration of a proxy.

  • 306 unused: this response code is no longer used, it is reserved. It was used in an earlier version of the HTTP 1.1 specification.

  • 307 Temporary Redirect: the server sent this reply directing the client to search for the requested resource in another URI with the same method that was used in the original request. It has the same semantics as the 302 Found code, with the exception that the user-agent should not change the HTTP method used: if a POST was used in the first request, a POST should be used in the second.

  • 308 Permanent Redirect: this code means that the resource is now permanently located in another URI, specified by the Location response header. It has the same semantics as the HTTP 301 Moved Permanently response code except that the user-agent should not change the HTTP method used: if a POST was used in the first request, a POST should be used in the second.

In 4xx I will put two very common and I leave a link to another question explaining two others and their diffidences (401 Unauthorized vs 403 Forbidden):

  • 400 Bad Request means that the request contains some error, may be a registration missing fill the name, the CPF is invalid (missing a number, for example), or something else that prevents the server processing because the client sent something wrong

  • 404 Not Found means that the resource (anything) that the customer sought does not exist, for example, if that question is deleted and I try to access it, that will be the answer

Last but not least, the 5xx:

  • 503 Service Unavailable usually happens when the server is under maintenance or overloaded and therefore the service is unspeakable

  • 505 HTTP Version Not Supported will be the answer if I make a request using HTTP2 to a server that only supports 1.1

The answers 400 and 500 are broader, some error that occurs either from the client or from the server, may have more specific status of the problem

Full list of HTTP response status (MDN)

Full list of HTTP response status (Wikipedia)

Original official documentation (rfc2068)

Link to the most current rfc of each status

  • Thank you very much, I took a look at my study project by Dev and could see these status, now I can better understand what kind of response my requests bring. I’ll even save your explanation.

  • 1

    I suggest taking a look at the links of HTTP methods, very useful, can also take a look at other HTTP questions

  • @Johncovv the fonts I posted in my reply contain all this which is in this copied and pasted reply.

  • @Is there a problem? Since I like the subject, I wanted to give a more complete answer and with my words, the only part I copied and pasted was the explanations of the 3xx status, and I mentioned this

1

HTTP

The Hypertext Transfer Protocol, acronym HTTP (in Portuguese Protocolo de Transferência de Hipertexto) is a communication protocol (on the application layer according to the OSI model) used for hypermedia, distributed and collaborative information systems. It is the basis for the World Wide Web (WWW).

SOURCE

Hypertext

Is the structured text using logical links (hyperlinks) between nodes containing text. The HTTP is the protocol to the hypertext exchange or transfer.

SOURCE

HTTP STATUS

The status codes for HTTP responses indicate whether a HTTP request was correctly completed. As answers are grouped into five (5) classes: information responses, success responses, redirects, client errors and server errors.

SOURCE - VISIT TO FIND OUT HTTP RESPONSE STATUS CODES AND WHAT THEY ARE FOR

Browser other questions tagged

You are not signed in. Login or sign up in order to post.