DEV Community

Cover image for HTTP Communication
QuecPython
QuecPython

Posted on

HTTP Communication

Introduction to HTTP

HTTP Protocol

HTTP (Hyper Text Transfer Protocol) is a communication protocol that allows clients to request and access server resources through URLs (Uniform Resource Locators). This protocol is based on the client-server model and organizes requests and responses in text format. The communication model is as follows:

HTTP is commonly used for web page
acquisition and form submission based on browsers, file upload and download, mobile applications, and data reporting for IoT devices.

Request and Response Messages

Request Message

The format of an HTTP request message is as follows:

  • Request line: The request line, consisting of the method, URL, and Version fields.
  • Method: Request method. Commonly used methods include GET, POST, PUT, HEAD, DELETE, etc. See details below.
  • URL: Uniform Resource Locator, used to identify the path of server resources.
  • Version: Protocol version, currently the commonly used version is HTTP/1.1.
  • Header lines: Request headers, consisting of one or more header fields, each with the format header field name: value.
  • Head field name: The name of the head field.
  • Value: The value of the header field.
  • Entity body: The message body, which is the main text of the message passed to the server.

Note that sp represents a space, cr lf represents a carriage return and line feed. There must be a blank line between the request headers and the message body, i.e., the blank line in the figure.

Here is an example of a request message:

Response Message

The format of an HTTP response message is as follows:

  • Version: Protocol version, currently the commonly used version is HTTP/1.1.
  • Status code: Response status code, details are provided below.
  • Phrase: Reason phrase.
  • Header lines: Response headers, consisting of one or more header fields, each formatted as header field name: value.
  • Head field name: The name of the head field.
  • Value: The value of the header field.
  • Entity body: The message body, which is the message content of the server response.

Note that sp represents a space, cr lf represents a carriage return and line feed. There must be a blank line between the response headers and the message body, as shown in the diagram.

Here is an example of a response message:

HTTP Request Methods

The commonly used HTTP methods include GET, POST, PUT, HEAD, and DELETE.

  • GET: The most commonly used method in HTTP requests, used to request resources from the server.
  • HEAD: Verify the validity of a URL and retrieve information such as the last modified date of a resource, which is similar to the GET method, but without returning the message body.
  • PUT: Upload resources to the server. Create a resource if no resource exists; otherwise, replace the resource.
  • POST: Modify server resources. Create a resource if no resource exists; otherwise, update the resource.
  • DELETE: Deletes a resource, the opposite of the PUT method.

HTTP Status Codes

The first line of a server's response message is the status line, which includes the status code and reason phrase to inform the client of the result of the request.

For more details about the HTTP protocol, please refer to the official documentation of the Internet Engineering Task Force (IETF).

The difference and relation between HTTP and TCP protocols:

_- HTTP is based on the TCP protocol.

  • TCP is a byte stream protocol, and it is difficult for users to determine the boundaries of business data. While HTTP has a specific message format, and message length can be specified by the Content-Length: ${Length} header, or the data encoding format can be specified by the Transfer-Encoding:chunked header. Both can identify message boundaries. QuecPython modules support parsing both Content-Length: ${Length} and Transfer-Encoding: chunked._

HTTP Application Examples

Query Weather
Amap Weather Query Open API:

εŒ—δΊ¬ can be replaced with the name of other cities in China.

Top comments (0)