- 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.
- arc42 by Example
- 樂學Web編程:網站制作不神秘
- Instant QlikView 11 Application Development
- Instant RubyMotion App Development
- JAVA程序設計實驗教程
- Yii Project Blueprints
- Android移動開發案例教程:基于Android Studio開發環境
- 小型編譯器設計實踐
- 軟件工程基礎與實訓教程
- JavaEE架構與程序設計
- 計算機應用基礎(Windows 7+Office 2010)
- Learning Google Apps Script
- AngularJS by Example
- Access 2010數據庫教程(微課版)
- Performance Testing with JMeter 3(Third Edition)