官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 宝应县| 珠海市| 陵川县| 鄂温| 平定县| 富裕县| 南康市| 饶平县| 道孚县| 贵州省| 台东市| 扎囊县| 荥经县| 郸城县| 阳江市| 安顺市| 玛纳斯县| 白沙| 广宁县| 峡江县| 天津市| 丁青县| 长沙县| 吴旗县| 年辖:市辖区| 和田县| 象州县| 克什克腾旗| 松桃| 日照市| 育儿| 海口市| 苏州市| 吴堡县| 乾安县| 水城县| 吉林省| 辉县市| 神农架林区| 宜兴市| 北流市|