HTTP Request Và HTTP Response

Tram Ho

HTTP Message

As we all know, HTTP is a client-server-based protocol that communicates between client and server based on a request-response pair, the client issues requests and the server responds to these requests.

HTTP Request

To begin exchanging data, the client initiates an HTTP session by opening a TCP connection to the HTTP server and then making a request to this server. Requests can be made in many ways, directly when the user clicks on a link in the browser or indirectly, such as a video attached to a website and the request to this website will result in a request to the video. that.


For example an HTTP Request

The beginning of the HTTP Request will be the Request-Line line consisting of 3 information:

  • Method: is the method that this HTTP Request uses, usually GET, POST, in addition to some other methods such as HEAD, PUT, DELETE, OPTION, CONNECT. In the example above, GET
  • URI: address identifier of the resource. In this case, the URI is / – ie the request for the original resource, if the request does not request a specific resource, the URI may be an *.
  • HTTP version: is the HTTP version in use, here is HTTP 1.1.

Next is the request-header fields, which allow the client to send additional information about the HTTP request message and the client itself. Some common schools such as:

  • Accept: content type that can be received from the response message. For example: text / plain, text / html …
  • Accept-Encoding: accepted compression types. For example: gzip, deflate, xz, exi …
  • Connection: Control options for the current connection. For example: keep-alive, Upgrade …
  • Cookies: HTTP Cookie information from the server.
  • User-Agent: information about the user agent of the user.

GET and POST methods

The two methods that are most used in HTTP requests are GET and POST

With GET, the query will be attached to the path of the HTTP request. For example: /? Username = "abc" & password = "def"

Some characteristics of the GET method:

  • GET requests can be cached, bookmarked and saved in the browser history.
  • GET requests are limited in length, because the length of the URL is limited.
  • GET requests should not be used with important data, only used to receive data.

In contrast, with POST, the query will be sent in the message body of the HTTP request, some characteristics of POST such as:

  • POST cannot be cached, bookmarked or saved in browser history.
  • POST is not limited in length.

Other methods

In addition to GET and POST, HTTP requests can have a number of other methods, such as:

  • HEAD: Same as GET but only sent back to HTTP headers.
  • PUT: Upload a description of a predefined URI.
  • DELETE: delete a predetermined resource.
  • OPTIONS: return the HTTP method that the server supports.
  • CONNECT: converts the HTTP request's connection to an HTTP tunnel connection.

HTTP Response

The HTTP response structure is similar to the HTTP request, except that instead of Request-Line, HTTP has a response with Status-Line. And like Request-Line, Status-Line also has three parts as follows:

  • HTTP-version: the highest HTTP version supported by the server.
  • Status-Code: Code of results returned.
  • Reason-Phrase: description of Status-Code.


For example an HTTP Response

HTTP Status Codes

Some common status-codes that the server returns to the client are as follows:

1xx: information Message: these status codes are only temporary, the client may not care.

2xx Successful: after successfully processing the client request, the server returns this status:

  • 200 OK: request succeeded.
  • 202 Accepted: The request has been received, but no results have been returned, telling the client to continue waiting.
  • 204 No Content: The request has been processed but no elements have been returned.
  • 205 Reset: same as 204 but this code also requires the client to reset the document view.
  • 206 Partial Content: The server only sends a piece of data, depending on the range of the client's range header.

3xx Redirection: The server informs the client to perform additional operations to complete the request:

  • 301 Moved Permanently: the resource has been completely moved to the Location address in the HTTP response.
  • 303 See other: the resource has been temporarily moved to the Location address in the HTTP response.
  • 304 Not Modified: The resource has not changed since the client last requested it, so the client can use it is cached.

4xx Client error: client error:

  • 400 Bad Request: the request is not in the correct format or syntax.
  • 401 Unauthorized: The client has not been authenticated.
  • 403 Forbidden: The client does not have access.
  • 404 Not Found: resource not found.
  • 405 Method Not Allowed: method not supported by the server.

5xx Server Error: server error:

  • 500 Internal Server Error: there was an error processing the server.
  • 501 Not Implemented: The server does not support the requested client function.
  • 503: Service Unavailable: The server is overloaded, or has a processing error.

References

https://www.stdio.vn/articles/http-request-va-http-response-202

Share the news now

Source : Viblo