- 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.
- JSP網絡編程(學習筆記)
- Spring Cloud Alibaba核心技術與實戰案例
- Redis入門指南(第3版)
- 算法基礎:打開程序設計之門
- C語言程序設計立體化案例教程
- YARN Essentials
- FFmpeg入門詳解:音視頻原理及應用
- Python面向對象編程:構建游戲和GUI
- SharePoint Development with the SharePoint Framework
- 零基礎入門學習Python
- The DevOps 2.5 Toolkit
- HTML5 APP開發從入門到精通(微課精編版)
- 智能手機APP UI設計與應用任務教程
- Orchestrating Docker
- Python 3 數據分析與機器學習實戰