The HTTP specification is a standard that we should follow when developing web-related projects. Here, I record the basic knowledge and common usage that I find useful.
The HTTP protocol (HyperText Transfer Protocol) is the most widely used network transmission protocol on the Internet, based on TCP/IP communication protocol to transmit data. All WWW files must comply with this standard. The default port number for HTTP is 80.
-
HTTP is connectionless: Being connectionless means that each connection handles only one request. After the server processes the client’s request and receives the client’s response, it disconnects. This method saves transmission time.
-
HTTP is media-independent: This means that any type of data can be sent via HTTP as long as the client and server know how to handle the data content. The client and server specify the appropriate MIME-type content type.
-
HTTP is stateless: The HTTP protocol is a stateless protocol. Stateless means that the protocol has no memory of transaction processing. The lack of state means that if subsequent processing requires previous information, it must be retransmitted, which may increase the amount of data transmitted with each connection. On the other hand, when the server does not need prior information, its response is faster.
HTTP 1.0 defines three request methods: GET, POST, and HEAD. HTTP 1.1 adds five request methods: OPTIONS, PUT, DELETE, TRACE, and CONNECT.
-
GET
-
Requests specified page information and returns the entity body.
-
HEAD
-
Similar to GET requests, but the response returned does not contain specific content, used to retrieve headers.
-
POST
-
Submits data to the specified resource for processing (e.g., submitting a form or uploading a file). The data is included in the request body. A POST request may result in the creation of new resources and/or modification of existing resources.
-
PUT
-
Transmits data from the client to the server to replace the content of the specified document.
-
DELETE
-
Requests the server to delete the specified page.
-
CONNECT
-
Reserved for proxy servers that can switch the connection to pipeline mode in the HTTP/1.1 protocol.
-
OPTIONS
-
Allows the client to view the server’s capabilities.
-
TRACE
-
Echoes the request received by the server, primarily used for testing or diagnosis.
-
Allow
-
Which request methods are supported by the server (e.g., GET, POST, etc.).
-
Content-Encoding
-
The encoding method of the document. Only after decoding can the content type specified in the Content-Type header be obtained.
-
Using gzip compression can significantly reduce the download time of HTML documents.
-
Content-Length
-
Indicates the length of the content. This data is only needed when the browser uses persistent HTTP connections.
-
Content-Type
-
Indicates what MIME type the following document belongs to.
-
Date
-
The current GMT time.
-
Expires
-
When the document should be considered expired and no longer cached.
-
Last-Modified
-
The last modification time of the document. The client can provide a date through the If-Modified-Since request header, which will be treated as a conditional GET; only documents with modification times later than the specified time will be returned, otherwise a 304 (Not Modified) status will be returned.
-
Location
-
Indicates where the client should go to retrieve the document.
-
Refresh
-
Indicates how long the browser should wait before refreshing the document, in seconds.
-
Note: This function is usually implemented by setting the in the HEAD area of the HTML page.
-
Note: The meaning of Refresh is “refresh this page or visit the specified page after N seconds” and not “refresh this page or visit the specified page every N seconds.” Therefore, continuous refresh requires sending a Refresh header each time, and sending a 204 status code can prevent the browser from continuing to refresh, regardless of whether it uses the Refresh header or .
-
Note: The Refresh header is not part of the official HTTP 1.1 specification, but an extension.
-
Server
-
The server name.
-
Set-Cookie
-
Sets a cookie associated with the page.
-
WWW-Authenticate
-
What type of authorization information should the client provide in the Authorization header? This header is required in responses containing a 401 (Unauthorized) status line.
Used to provide information to the browser and server, indicating the resource type corresponding to this URL, which should match properly and not misrepresent the content.
Note: Only the more common ones are listed here.
File Extension | Content-Type (Mime-Type) |
---|---|
.css | text/css |
.gif | image/gif |
.htm | text/html |
.html | text/html |
.jpeg | image/jpeg |
.jpg | image/jpeg |
.js | application/x-javascript |
.ico | image/x-icon |
.mp3 | audio/mp3 |
.mp4 | video/mpeg4 |
.mpeg | video/mpg |
.mpg | video/mpg |
application/pdf | |
.png | image/png |
.tif | image/tiff |
.tiff | image/tiff |
.torrent | application/x-bittorrent |
.wav | audio/wav |
.xhtml | text/html |
Whether returning HTML or API responses, we should follow the HTTP status code specification as much as possible; there is no need to create our own set. In simple terms, HTTP status codes consist of three decimal digits, the first digit indicates the category, and the last two are the number, with a total of five major categories.
Note: The bolded ones are commonly used status codes.
1XX Informational
The server has received the request and the requester needs to continue the operation.
-
100 Continue
-
Continue. The client should continue its request.
-
101 Switching Protocols
-
Switching Protocols. The server switches protocols based on the client’s request. Only higher-level protocols can be switched to, for example, switching to a new version of the HTTP protocol.
2XX Success
The operation has been successfully received and processed.
-
200 OK
-
The request was successful. Generally used for GET and POST requests.
-
201 Created
-
Created. The request was successful, and a new resource has been created.
-
202 Accepted
-
Accepted. The request has been accepted but not yet processed.
-
203 Non-Authoritative Information
-
Non-Authoritative Information. The request was successful, but the returned meta information is not from the original server, but a copy.
-
204 No Content
-
No Content. The server successfully processed the request but did not return any content. This ensures that the browser continues to display the current document without updating the webpage.
-
205 Reset Content
-
Reset Content. The server successfully processed the request, and the user agent (e.g., browser) should reset the document view. This return code can clear the browser’s form fields.
-
206 Partial Content
-
Partial Content. The server successfully processed part of the GET request.
3XX Redirection
Further action is required to complete the request.
-
300 Multiple Choices
-
Multiple Choices. The requested resource can include multiple locations, and the response can return a list of resource characteristics and addresses for the user agent (e.g., browser) to choose from.
-
301 Moved Permanently
-
Moved Permanently. The requested resource has been permanently moved to a new URI, and the returned information will include the new URI, and the browser will automatically redirect to the new URI. Any new requests in the future should use the new URI instead.
-
302 Found
-
Found. Similar to 301, but the resource is only temporarily moved. The client should continue using the original URI.
-
303 See Other
-
See Other. Similar to 301. Use GET and POST requests to view.
-
304 Not Modified
-
Not Modified. The requested resource has not been modified, and when the server returns this status code, it will not return any resource. The client typically caches accessed resources and indicates a preference to only return resources modified after a specified date by providing a header.
-
305 Use Proxy
-
Use Proxy. The requested resource must be accessed through a proxy.
-
306 Unused
-
HTTP status code that has been deprecated.
-
307 Temporary Redirect
-
Temporary Redirect. Similar to 302. Redirect using GET requests.
4XX Client Error
The request contains a syntax error or cannot be completed.
-
400 Bad Request
-
The syntax of the client’s request is incorrect, and the server cannot understand it.
-
401 Unauthorized
-
The request requires user authentication.
-
402 Payment Required
-
Reserved for future use.
-
403 Forbidden
-
The server understands the client’s request but refuses to execute it.
-
404 Not Found
-
The server cannot find the resource (webpage) based on the client’s request. By using this code, website designers can set a personalized page saying “The resource you requested cannot be found.”
-
405 Method Not Allowed
-
The method in the client’s request is prohibited.
-
406 Not Acceptable
-
The server cannot complete the request based on the content characteristics of the client’s request.
-
407 Proxy Authentication Required
-
The request requires proxy authentication, similar to 401, but the requester should authorize through the proxy.
-
408 Request Time-out
-
The server waited too long for the client to send the request, resulting in a timeout.
-
409 Conflict
-
The server may return this code when completing the client’s PUT request, indicating that a conflict occurred while processing the request.
-
410 Gone
-
The resource requested by the client no longer exists. 410 differs from 404; if the resource previously existed and has now been permanently deleted, code 410 can be used, and website designers can specify the new location of the resource using code 301.
-
411 Length Required
-
The server cannot process the request information sent by the client without a Content-Length.
-
412 Precondition Failed
-
The prerequisite condition of the client’s request information is incorrect.
-
413 Request Entity Too Large
-
The server cannot process the request because the entity is too large, and thus refuses the request. To prevent continuous requests from the client, the server may close the connection. If the server is temporarily unable to process, it will include a Retry-After response message.
-
414 Request-URI Too Large
-
The requested URI is too long (the URI is usually the URL), and the server cannot process it.
-
415 Unsupported Media Type
-
The server cannot process the media format attached to the request.
-
416 Requested range not satisfiable
-
The range requested by the client is invalid.
-
417 Expectation Failed
5XX Server Error
An error occurred on the server while processing the request.
-
500 Internal Server Error
-
Internal server error, unable to complete the request.
-
501 Not Implemented
-
The server does not support the requested functionality and cannot complete the request.
-
502 Bad Gateway
-
The server acting as a gateway or proxy received an invalid request from the upstream server.
-
503 Service Unavailable
-
Due to overload or system maintenance, the server is temporarily unable to process the client’s request. The length of the delay can be included in the server’s Retry-After header information.
-
504 Gateway Time-out
-
The server acting as a gateway or proxy did not receive a timely response from the upstream server.
-
505 HTTP Version not supported
-
The server does not support the version of the HTTP protocol requested and cannot complete processing.
Master… After reading this, please give a thumbs up before leaving.