Interviewer: What are the common HTTP request headers?

Interviewer: What are the common HTTP request headers?

[[401820]]

This article is reprinted from the WeChat public account "JS Daily Question", the author is Huihui. Please contact the JS Daily Question public account to reprint this article.

1. What is

HTTP header fields refer to the message header part in the request and response messages of the Hypertext Transfer Protocol (HTTP)

They define the parameters of an operation within a HTTP transaction.

HTTP header fields can be defined as needed, so non-standard header fields may be found on web servers and browsers.

The following is a request header for an HTTP request:

  1. GET /home.html HTTP/1.1
  2. Host: developer.mozilla.org
  3. User -Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:50.0) Gecko/20100101 Firefox/50.0
  4. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  5. Accept-Language: en-US,en;q=0.5
  6. Accept-Encoding: gzip, deflate, br
  7. Referer: https://developer.mozilla.org/testpage.html
  8. Connection : keep-alive
  9. Upgrade-Insecure-Requests: 1
  10. If-Modified-Since: Mon, 18 Jul 2016 02:36:04 GMT
  11. If-None-Match: "c561c68d0ba92bbeb8b0fff2a9199f722e3a621a"  
  12. Cache-Control: max -age=0

2. Classification

Common request fields are shown in the following table:

Field Name illustrate Example
Accept Acceptable response content types (Content-Types) Accept: text/plain
Accept-Charset Acceptable character sets Accept-Charset: utf-8
Accept-Encoding List of acceptable encodings Accept-Encoding: gzip, deflate
Accept-Language A natural language list of acceptable responses Accept-Language: en-US
Authorization Authentication information for HTTP authentication Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
Cache-Control Used to specify instructions that all cache mechanisms in this request/response chain must comply with Cache-Control: no-cache
Connection The type of connection this browser prefers Connection: keep-alive Connection: Upgrade
Cookie A HTTP cookie sent by the server via Set-Cookie (described below) Cookie: $Version=1; Skin=new;
Content-Length The length of the request body as an octet array (8-bit bytes). Content-Length: 348
Content-Type The multimedia type of the request body Content-Type: application/x-www-form-urlencoded
Date The date and time the message was sent Date: Tue, 15 Nov 1994 08:12:31 GMT
Expect Indicates that the client requires the server to perform specific actions Expect: 100-continue
Host The server's domain name (for virtual hosting) and the TCP port number that the server is listening on Host: en.wikipedia.org:80 Host: en.wikipedia.org
If-Match The corresponding operation is performed only when the entity provided by the client matches the corresponding entity on the server. The main purpose is to use it in methods such as PUT to update a resource only if the resource has not been modified since the user last updated it. If-Match: "737060cd8c284d8af7ad3082f209582d"
If-Modified-Since Allows to return 304 Not Modified if the corresponding content has not been modified If-Modified-Since: Sat, 29 Oct 1994 19:43:31 GMT
If-None-Match Allows to return 304 Not Modified if the corresponding content has not been modified If-None-Match: "737060cd8c284d8af7ad3082f209582d"
If-Range If the entity has not been modified, send me the part or parts that I am missing; otherwise, send the entire new entity. If-Range: "737060cd8c284d8af7ad3082f209582d"
Range Requesting only part of an entity Range: bytes=500-999
User-Agent The browser's browser identification string User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/21.0
Origin Make a request for Cross-Origin Resource Sharing Origin: http://www.example-social-network.com

3. Usage scenarios

By coordinating the request header and response header, the following functions can be implemented in some scenarios:

Negotiation Cache

Negotiation cache is managed using two pairs of request and response headers: [Last-Modified, If-Modified-Since] and [ETag, If-None-Match]

Last-Modified indicates the date when the local file was last modified. The browser will add If-Modified-Since (the value of Last-Modified returned last time) to the request header to ask the server whether the resource has been updated after that date. If so, the new resource will be sent back.

Etag is like a fingerprint. Any change in the resource will cause the ETag to change, regardless of the last modification time. ETag can ensure that each resource is unique.

The If-None-Match header will send the Etag returned last time to the server, asking whether the Etag of the resource has been updated. If there is a change, a new resource will be sent back.

Forced caching does not require sending requests to the server. It determines whether the strong cache is hit based on the request headers expires and cache-control.

The flowchart of mandatory caching and negotiated caching is as follows:

Session State

Cookies are small text files that are stored on the user's local terminal by some websites in order to identify the user. They are determined by the response header set-cookie.

As a small text data that generally does not exceed 4KB, it consists of a name (Name), a value (Value) and several other optional attributes used to control the validity period, security, and scope of use of the cookie.

Cookies are mainly used in the following three aspects:

Session state management (such as user login status, shopping cart, game scores, or other information that needs to be recorded)

Personalization settings (such as user-defined settings, themes, etc.)

Browser behavior tracking (such as tracking and analyzing user behavior, etc.

References

https://zh.wikipedia.org/wiki/HTTP header fields

https://github.com/amandakelake/blog/issues/41

<<:  What is the difference between a free SSL certificate and a paid one?

>>:  Borei Data 2021 strategy release tour opens up innovative paths for IT operations and maintenance

Blog    

Recommend

Six IT trends to watch in 2023

Businesses and society at large continue to turn ...

What is the difference between artificial intelligence and machine learning?

【51CTO.com Quick Translation】 Artificial intellig...

What are the differences between HTTP and HTTPS besides security?

HTTP and HTTPS are two common network protocols, ...

Lightwave OFC 2025 Outlook: Quantum will take center stage

Beijing time, February 19 (Shui Yi) As the world&...

Talk about the past and present of programmable network elements

[[402793]] This article is reprinted from the WeC...

7 key features of 5G mobile phones

1. Support high-power terminals Compared with bas...

2018 World Internet of Things Expo press conference held in Beijing

Today, the global Internet of Things era is leadi...

CloudCone: $1.99/month KVM-768MB/15GB/3TB/Los Angeles MC Data Center

CloudCone's 2021 flash sale has started again...

5G: Number of terminal connections exceeds 200 million

2020 is the first year of large-scale constructio...