Http is a abbreviation of Hypertext Transfer Protocol, it runs in the application layer of network.
Hypertext means expanded text, we can put html content in it, and html content can provide a link refer to other text content(hyperlink).
The most common scene to use http in our life is browsing website on our computer and use application on mobile phone, the work mechanism is this:
- input url in computer browser
- computer browser build http request message
- http message send to goal server via DNS server
- server return response message
- computer browser analyze response message and rending content of response message via render engine
- click or open user interface
- application build request message in code
- send request message to server via DNS server
- server return response message
- application received response message and analyze it(in code) and do their own work.
The general flow of those two scenes is similar.
For example, this url:
It can be divided to three parts, protocol type, host address and path
https is protocol type
github.com is host address
/wongay is path
Request message can be divided into three parts: Request Line、Headers and Body.
- Request Line: it must contains request method [POST], path [/wongzy] and protocol version [http/1.1]
- Header: it must contains host [github.com], and other header is optional
- body: different from other two parts(Request Line and Header), it can be empty
Useful request methods includes several methods and their standard as follows:
- GET: 1. for request resource 2. do not amend service resource 3. request message do not have body
- POST: 1. used to put or amend resources in server 2. request message has body to put or amend resources
- PUT: 1. used to amend resources in server 2. request message has body to amend resources
- DELETE: 1. used to delete body 2. request message do not have body
- HEAD: 1. to get resources’ message 2. request message do not have body 3. response message do not have body
Response message can be divided into three parts: Status Line、 Header and Body.
- Status Line: it must contains protocol[http/1.1], status code , and response message [ok]
- Header: depend on server
- body: it can be empty
Common code includes five types, and their sense as follows:
- 1XX: temporary message，for example : 100[continue sending], 101[changing protocol]
- 2XX: request successfully code, for example : 200[OK], 201[build successfully]
- 3XX: redirecting, for example: 301[moved forever], 302[moved temporary],304[content not change]
- 4XX: client error, for example: 400[client request error]、401[authentication fail]、403[forbidden]、404[resource not fund]
- 5XX: server error, for example: 500[internal error]
Header is a important construction of Http message, it can be regard as a content summary, it has a official name, metadata.
it mainly includes those types:
Used in request message, to find goal server via mainframe.
To declare content’s type, included：
Plain text form
Binary resources, and those resources can be divided by boundary
application/json , image/jpeg , application/zip …
Used to response as soon as possible, can not use together with content-length, body can transfer by chunk， and it would give serial number in body to declare whether data is transferred finish.
Give url after redirect.
Give what platform(computer or phone) user used.
Declare resource data range in body, used to breakpoint resume and multithreading download.
Used in response message, it declare data can be divided by which unit, like byte.
Used in request message, it declare the range of data client requested
Used in response message, declare the range of data in body.
Declare whether data can be cached in middle point.
Used in request message, declare which type of content client can accept, for example : text/html.
Used in request message, declare which charset of content client can accept, for example : utf-8.
Used in request message, declare which encoding type client can accept, for example : gzip.
Declare content’s encoding type of body.
In Wiki， Rest means Representational state transfer， it is a really abstract concept. Roy Filed come up with Rest based on the exist of HTTP，Rest is extracted from http.
To use Rest correctly, we should obey http’s standard, but unfortunately, a large number of chinese company do not obey this(I do not know foreign company), event in many international company, they use method in a incorrect way, use POST do every thing in request message.