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

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.

主站蜘蛛池模板: 安多县| 邯郸市| 调兵山市| 平阴县| 沈阳市| 合阳县| 施甸县| 扬中市| 东乡族自治县| 施秉县| 鲁山县| 托克托县| 墨竹工卡县| 沅江市| 定日县| 界首市| 武汉市| 城固县| 游戏| 泗水县| 文化| 彩票| 通渭县| 和田县| 原平市| 漳州市| 桃源县| 武胜县| 永川市| 伊宁县| 丁青县| 龙泉市| 磴口县| 罗平县| 郸城县| 吉安市| 新疆| 安阳市| 平陆县| 渝北区| 嵊泗县|