HTTP learning Notes 1
Reference links:
- Parabola course
- HTTP Request basics – the difference between GET, POST, and PUT in HTTP
GET, POST, PUT, and DELETE are different
GET
- Used to obtain resources
- Server data is not modified, just retrieved
- Don’t send the body
- Idempotent (the emphasis here is that once and N times have the same side effects, not that every GET has the same result.)
GET /user/1? Gender = sex HTTP / 1.1 Host: api.github.comCopy the code
The code for Retorfit
@GET("/user/{id}")
Call<User> getUser(@Path("id") String id,@Query("gender") String gender)
Copy the code
POST
- Used to add or modify resources
- The content sent to the server is written inside the body
- Non-idempotent (multiple calls have different results, such as creating a user more than once for network or other reasons, so that multiple users can be created.)
- Generally used to create resources
PUT
- In theory, it is only used to modify resources (in practice, it is the same as POST)
- The content sent to the server is written inside the body
- Idempotent (PUT ID /456 creates a user with ID 456. Multiple calls create the same result, so PUT is idempotent.)
- Generally used to update resources
DELETE
-
Used to delete a resource
-
Don’t send the body
-
Idempotent (call once and N times have the same side effect on the system, that is, delete the post with ID 4231; Therefore, the caller can call or refresh the page multiple times without fear of causing an error.)
PATCH
- Used to update local data (PUT is the whole update)
- Don’t send the body
HEAD
- Much like GET, the request returned has no body
- Return to have the HEAD
The difference between POST and PUT
Technically there is no difference between POST and PUT. There are only semantic differences
HEAD is different from GET
The HEAD response has no body, and the GET response has body. H
Http Header partial parsing
The Http transfer process is essentially just text in format (the body part may be binary data).
Host
GET /users/1 HTTP/1.1
Host: api.github.com
Copy the code
The Host function is not used to locate the Host, the Host IP address is located before sending data
A Host is a domain name used to show the Host which server to assign.
Content-type (Transport type)
There are four main categories:
-
Text/HTML General web pages, etc
-
X-www-form-urlencoded normal form format, can only transmit string data
Submission of a plain text form on a Web page.
- The submission mode of a multipart/form-data Web page containing binary files.
4. application/json , image/jpeg , application/zip …
Single-item content (text or non-text) for Web Api responses or POST/PUT requests
Submit A JSON example
Content-Length
Specify the length of the Body (bytes)
Chunked (Chunked Transfer Encoding)
Used when the content length has not yet been determined when the response is initiated. And content-length are not used at the same time. The purpose is to respond early and reduce user waiting.
Location
Specifies the destination URL for the redirect
User-Agent
The user agent is the person who actually sends the request and receives the response, such as a mobile browser or a mobile App.
Range/accept-range – Retrieves data by Range
Accept-range: bytes If this parameter is present in the response packet, the server supports data collection in bytes. Range: bytes=- If this parameter is present in the request packet, it indicates the data to be collected. Content-range :-/total If this parameter is present in the response packet, it indicates the data to be sent
Function: discontinuous transmission, multithreading download.
Other Headers
- Accept: Indicates the data type accepted by the client. Such as text/HTML
- Accept-charset: character set accepted by the client. Such as utf-8
- Accept-encoding: Indicates the Encoding type accepted by the client. Such as gzip
- Content-encoding: Indicates the compression type. Such as gzip
The Cache and Buffer
Cache
Function: Caches data on clients or intermediate network nodes to reduce the frequency of retrieving data from servers and improve network performance. Buffer data, etc.
Buffer
Function: Buffer data. For example, if only part of the server data is transmitted, buffer it first and then process it after all data is transmitted
Definition and role of REST
Reference links:
What is the REST
The definition of REST is controversial and there is no consensus. In general, REST HTTP is the proper use of HTTP.
Include:
-
stateless
-
Use the format of the resource to define the URL
-
Use method formally to define network request operations
-
Use the Status code formally to indicate the status of the response
-
Other design guidelines that conform to the HTTP specification