Preface:

HTTP is the abbreviation of Hyper Text Transfer Protocol. Hypertext is a representation of a resource that is transferred from a WEB server to a local browser for presentation. Restful architecture, which is the architecture that HTTP designers follow. The full name is REpresentational State Transfer, which translates to REpresentational State Transfer. In the words of teacher @Ivony:

URL locates resources and describes operations using HTTP verbs (GET,POST,DELETE,DETC).

It is also a shift in describing the state of the resource. Since a resource has multiple states, clients need to negotiate when receiving a transfer of a resource. For example, when accessing a URL in China, the resources received may be in Chinese, while when accessing the same URL in other countries, the resources received may be in their own language.

Content negotiation

The resource that each URL points to can be anything and can be expressed in many different ways. For example, a document can be translated in different languages, in different media formats, and in different compression encodings for different browsers

There are two ways to negotiate content

  • Proactive content negotiation: A client requests the required representation form in the request headers, and the server provides specific representation based on these request headers.

    For example:

Accept-language :en indicates that the client requires the URI to be In Chinese. Accept-encoding :br Indicates that the client can Accept the COMPRESSION mode of br.

After receiving the request from the client, the server returns a response that meets the request.

  • Reactive content negotiation: the server returns 300Multiple Choices or 406 Not Acceptable, and the client selects a statement URI to use.

    For example:



    The client sends the request to the server, which decides it can’t be too arbitrary and returns a list of 300 response codes for the client to choose from.

    The client then selects a URL, such as URLe, from the returned list. Access the server again and the server returns the correct response.

Common elements of negotiation

  • Quality factor Q: quality of content, priority of acceptable types 1. Content quality. For example, if the client makes a request for an image that is only for quick viewing, it can do a very high compression ratio and the quality Q can be low. If the image is used for medical purposes, the Q is larger, because medical photo blurrout cannot be tolerated to lose a lot of detail. 2. Priority of acceptable types. For example, there is a URI that supports both Chinese and English. But if you want to display Chinese first, you can use Q to represent it.

  • Accept: text/ HTML,application/ XHTML + XML,application/ XML; Q = 0.9 * / *; 2, Axxept: text/ HTML,application/ XHTML + XML,application/ XML; 1 = 0.9, the image/webp image/apng, * / *; Q = 0.8, application/signed – exchange; v=b3

  • Accept-encoding: gzip. Deflate, BR

  • Accept-language: zh-cn,zh; Q = 0.9, en – US; Q = 0.8, en. 2, Accept-language: zh-cn,zh; Q = 0.8, useful – TW; Q = 0.7, useful – HK; Q = 0.5, en – US; Q = 0.3, en. Q = 0.2

Metadata header for resource representation

  • Media Type and encoding Content-Type: text/ HTML; chareset=utf-8
  • Content-encoding: gzip
  • Content-language: de-de, en-CA

conclusion

Content negotiation determines whether the server generates a different HTTP response body to transmit to the client.

note

For more articles, please go to Github, if you like, please click star, which is also a kind of encouragement to the author