HTTP basic knowledge

Source: Internet
Author: User

1. HTTP Request message Format

HTTP Request messages are divided into three partial request lines , request headers , and request Bodies , in a format


HTTP Request message Format

Note: Part of the article also divides the HTTP request message into two parts: the request header and the request body, and the first behavior request line of the request header.

1.1 Request Lines

Request line is divided into three parts: request method , request address and protocol and version , end with CRLF ( \r\n ).
There are 8 types of request methods defined by http/1.1:get, POST, PUT, DELETE, PATCH, HEAD, OPTIONS, TRACE, two of the most common get and POST, In the case of a restful interface, get, POST, DELETE, PUT are generally used.

Before you know the request address, let's look at the URL's composition:


Url.png


Path is a later part of the URL host that contains the query String, such as:

URL PATH
http://http://www.jianshu.com/ /
http://localhost:8080/index.php?id=1234 /index.php?id=1234
Http://weibo.com/920507888/home /920507888/home

Sample Request message:


sample HTTP Request message Format2. HTTP Response message Format

The format of the HTTP response in addition to the status line (the first line) and the request line is not the same, the other in the format is the same, but the difference between the elimination of the state line and the request line, from the header or can distinguish the HTTP request and HTTP response, how to distinguish between the front of the common header.


HTTP_ Response

Details of the status code (that is, the response code in) can be viewed in detail in the HTTP status code

To show the HTTP response more visually, the following example, from nc 1270.0.1:80 << EOF there, EOF is a simple HTTP request.


HTTP requests and Responses3. Header

The header can be used to pass some additional information in the format: 键: 值 , notice that there is a space after the colon ! Such as:

Content-Length: 1024Content-Type: text/plain
3.1 Common common headers for requests and responses
name function
Content-type Types of request body/response body, such as: Text/plain, Application/json
Accept Description of the received type, can be multiple values, , separated by (half-width comma)
Content-length Length of the request body/Response body, Unit bytes
Content-encoding The encoding format of the request body/response body, such as Gzip,deflate
Accept-encoding Inform the other party of the content-encoding we accept
ETag Identifies, and, mates for the current resource Last-Modified If-None-Match If-Modified-Since for cache control
Cache-control The value is General no-cache or max-age=XX , xx is an integer, indicating the resource cache validity period (seconds)
3.2 Common Requests Header
name function
Authorization Used to set up identity authentication information
User-agent User identities, such as: OS and browser type and version
If-modified-since Value is the value returned by the previous server to Last-Modified confirm that a resource has been changed and has not changed (304) to read from the cache
If-none-match Value is the ETAG value returned by the previous server, and will typically If-Modified-Since appear together with
Cookies Existing cookies
Referer Indicates which address the request refers to, such as when you jump from page A to page B, the value is the address of page a
Host Requested host and port number
3.3 Common response Headers
name function
Date Date of the server
Last-modified The last time the resource was modified
Transfer-encoding The value is generally chunked, in the case of content-length cannot be determined, indicating that the server does not know the size of the response version of the data, and generally also appear the Content-Encoding response header
Set-cookie Set cookies
Location Redirect to another URL, such as entering the browser input baidu.com carriage return, will automatically jump to https://www.baidu.com, which is controlled by this response header
Server Background server
4.3 Forms of the request body

Depending on the scenario, the request body of the HTTP request has three different forms.

The first type:

Mobile developers are common, the request body is any type, the server does not parse the request body, the request body processing needs its own parsing, such as the POST JSON is this class.


HTTP request, form a second type:

The second and third have a fixed format, which is the first two of the server-side developers to learn. The format requirement here is the format requirement for query string in the URL: multiple key-value pairs are connected by a & connection, the key and value are = concatenated, and only ASCII characters are used, and non-ASCII characters need to be UrlEncode encoded.


HTTP request, form two
The second type of exampleThe third type:

The request body of the third request body is divided into several parts, the file upload will be used, this format should be used in the mail transmission, each field/file is boundary ( Content-Type in the specified) into a separate segment, each paragraph with the -- beginning of boundary, Then is the description header of the paragraph, the description of the header after the empty line of content, the end of the request is boundary after the addition -- , structure see:


HTTP request, form three

Whether or not the key to a file is Content-Disposition included filename , because the file has a different type, so also use the Content-Type type that indicates the file, if you do not know what type of value can be to application/octet-stream indicate that the file is a binary file, if it is not a file Content-Type can be omitted.

The original text of the request for an upload with a file:


example of a third request body

Attention:
The second to third request body needs to match the specific content-type request header, such as:
The second kind of coordinationContent-Type:application/x-www-form-urlencoded
The third kind of cooperation Content-Type: multipart/form-data; boundary={boundary} ,
* The above form-data can also be mixed ,,, alternative digest parallel , but I only use theform-data
If the two do not match, then the server will not parse the request body, that is, only as the first case!

A form or a mock -up form refers to the second and third type (Multipart/form-data)

5. Recommended Debug Tool 5.1 CURL

CURL is quite powerful command-line tool, basically you know the upper layer of the protocol it is supported, the concrete use of the method is discovered by itself, in order to initiate an HTTP GET request and print the request and response details.


CURL5.2 Bat

bat is Astaxie (Xie Mengjun) with Golang developed similar Curl command line API debugging tool, can easily print out HTTP request and response, also can highlight header, format JSON and other functions, very easy to use, API mode artifact.


Bat5.3 NC

NC is a shorthand for netcat, known as "the Swiss Army Knife in Network Tools", but I personally use it as a socket, often used to print a variety of requests, of course, it does not only function, you can also use it to launch a variety of requests, the previous mode POP3 is also used it, Just request the message to write itself, there is a figure above "HTTP request and Response" is done with NC.

6. The composition diagram of HTTP

HTTP basic knowledge

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.