- Creative Projects for Rust Programmers
- Carlo Milanesi
- 514字
- 2021-06-18 19:02:00
Essential background theory and context
Previously, we said that a RESTful service is based on the HTTP protocol. This is a rather complex protocol, but its most important parts are quite simple. Here is a simplified version of it.
The protocol is based on a pair of messages. First, the client sends a request to the server, and after the server receives this request, it replies by sending a response to the client. Both messages are in American Standard Code for Information Interchange (ASCII) text, and so they are easily manipulated.
The HTTP protocol is usually based on the TCP/IP protocol, which guarantees that these messages arrive at the addressed process.
Let's see a typical HTTP request message, as follows:
GET /users/susan/index.html HTTP/1.1
Host: www.acme.com
Accept: image/png, image/jpeg, */*
Accept-Language: en-us
User-Agent: Mozilla/5.0
This message contains six lines because there is an empty line at the end.
The first line begins with the word GET. This word is the method that specifies which operation is requested. Then, there is a Unix-style path of a resource, and then the version of the protocol (here, it is 1.1).
Then, there are four lines containing rather simple attributes. These attributes are name headers. There are many possible optional headers.
What follows the first empty line is the body. Here, the body is empty. The body is used to send raw data—even a lot of data.
So, any request from the HTTP protocol sends a command name (the method) to a specific server, followed by an identifier of a resource (the path). Then, there are a few attributes (one per line), then an empty line, and, finally, the possible raw data (the body).
The most important methods are detailed as follows:
- GET: This requests a resource to be downloaded from the server (typically an HTML file or an image file, but also any data). The path specifies where the resource should be read.
- POST: This sends some data to the server that the server should consider as new. The path specifies where to add this data. If the path identifies any existing data, the server should return an error code. The contents of the data to post are in the body section.
- PUT: This is similar to the POST command, but it is meant to replace existing data.
- DELETE: This requests the resource to be removed specified by the path. It has an empty body.
Here is a typical HTTP response message:
HTTP/1.1 200 OK
Date: Wed, 15 Apr 2020 14:03:39 GMT
Server: Apache/2.2.14
Accept-Ranges: bytes
Content-Length: 42
Connection: close
Content-Type: text/html
<html><body><p>Some text</p></body></html>
The first line of any response message begins with the protocol version, followed by the status code both in text format and in numeric format. Success is represented by 200 OK.
Then, there are several headers—six, in this example—then an empty line, and then the body, which may be empty. In this case, the body contains some HTML code.
You can find more information regarding the HTTP protocol at: https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol.
- 零基礎(chǔ)搭建量化投資系統(tǒng):以Python為工具
- 深入實(shí)踐Spring Boot
- Mastering QGIS
- Hands-On C++ Game Animation Programming
- Learning ELK Stack
- 基于Swift語言的iOS App 商業(yè)實(shí)戰(zhàn)教程
- HTML5入門經(jīng)典
- 程序是怎樣跑起來的(第3版)
- ASP.NET Core 2 Fundamentals
- 持續(xù)集成與持續(xù)交付實(shí)戰(zhàn):用Jenkins、Travis CI和CircleCI構(gòu)建和發(fā)布大規(guī)模高質(zhì)量軟件
- JavaScript程序設(shè)計(jì)(第2版)
- 后臺開發(fā):核心技術(shù)與應(yīng)用實(shí)踐
- HTML5+CSS3+JavaScript 從入門到項(xiàng)目實(shí)踐(超值版)
- JavaScript Concurrency
- Android初級應(yīng)用開發(fā)