了解HTTP:REST的骨干

To understand the REST architecture, it is very important that we understand the HTTP clearly because all the http protocols are used while building a REST API.

要了解REST架构,非常重要的一点就是要清楚地了解HTTP,因为在构建REST API时会使用所有http协议。

HTTP is all over the internet. Every time when we hit a URL in our browser, an HTTP request is sent to the webserver and we receive the HTML content in response. An important thing to note here is that, while the website is usually for the human consumption the REST Api is usually for application consumption.

HTTP遍布整个互联网。 每当我们在浏览器中点击一个URL时,一个HTTP请求就会发送到Web服务器,并且我们会收到HTML内容作为响应。 这里要注意的重要一点是,虽然该网站通常供人类使用,但REST Api通常供应用程序使用。

Therefore, while requesting the data from a website, the data should be in a browser readable format, which is HTML, while in case of the REST API, response can be anything like XML/JSON or any other media type.

因此,在从网站请求数据时,数据应采用浏览器可读的格式,即HTML ,而在使用REST API的情况下,响应可以是XML / JSON或任何其他媒体类型。

Since REST is very much inspired by the HTTP, HTTP can be said as the backbone of the REST.

由于REST非常受HTTP启发,因此可以说HTTP是REST的骨干。

HTTP is a TCP/IP based communication protocol, which is used to deliver data (HTML files, image files, query results, etc.) accross the World Wide Web.

HTTP是基于TCP / IP的通信协议,用于在万维网上传递数据(HTML文件,图像文件,查询结果等)。

One very important point about REST is, that it is not connected to web, and will return no response when tried to access via a browser, although it is based on HTTP.

关于REST的一个非常重要的一点是,尽管它基于HTTP,但它未连接到Web,并且在尝试通过浏览器进行访问时不会返回任何响应。

HTTP的基本功能 (Basic Features of the HTTP)

As we have already learnt HTTP is a protocol which allows us to send files back and forth on the web, which involves a client and a server. HTTP is text based, which makes it easier to monitor.

正如我们已经了解到的,HTTP是一种协议,它允许我们在Web上来回发送文件,其中涉及客户端和服务器。 HTTP是基于文本的,因此更易于监视。

The basic features of HTTP are:

HTTP的基本功能是:

  1. HTTP is connectionless.

    HTTP是无连接的

  2. HTTP is media independent, which means any type of data can be sent through the http.

    HTTP是独立于媒体的 ,这意味着可以通过http发送任何类型的数据。

  3. HTTP is stateless, neither the server nor the client keeps a track of the last request.

    HTTP是无状态的 ,服务器和客户端都不跟踪最后一个请求。

HTTP makes use of the Uniform Resource Identifier (URI) to identify any given resource and establish a connection. HTTP request and response, use a generic message format of RFC 822 for transferring the data.

HTTP利用统一资源标识符(URI)来标识任何给定资源并建立连接。 HTTP请求和响应,使用RFC 822的通用消息格式来传输数据。

The generic message of any HTTP request and response, consists of the following four items:

任何HTTP请求和响应的通用消息均由以下四项组成:

  1. A start line

    起跑线

  2. Zero or more Headers

    零个或多个标题

  3. Empty line indicating the end of the header fields

    空行指示标题字段的结尾

  4. and, Optional message Body.

    并且,可选的消息

HTTP header holds the metadata and information about the HTTP method, while the body contains the data that we want to send to the server.

HTTP标头保存元数据和有关HTTP方法的信息,而正文包含我们要发送到服务器的数据。

We can check the HTTP requests as we surf on the Internet. If you use Chrome Browser, press F12 in Windows, and Command + Option + I in Mac OS to open developers tool. Under the Network tab, you can see the HTTP request made, and the response received along with the headers.

我们可以在Internet上浏览时检查HTTP请求。 如果您使用Chrome浏览器,请在Windows中按F12 ,在Mac OS中按Command + Option + I打开开发人员工具。 在“ 网络”标签下,您可以看到发出的HTTP 请求以及收到的响应以及标头

Checking HTTP request and response in Browser

REST和HTTP (REST and HTTP)

URL stands for Uniform Resource Locator. Yes, a webpage is also a resource and is loaded by sending a request to the associated URL.

URL代表统一资源定位符。 是的,网页也是一种资源,并且通过向关联的URL发送请求来加载网页。

Let's take a general example, an application which manages school's students data.

让我们举一个一般的例子,一个管理学校学生数据的应用程序。

/students

The above URL will return all the students data in response. While,

上面的URL将返回所有学生数据作为响应。 而,

/students/viraj

will only return data for the student with name viraj, if its unique.

如果名称唯一,将仅返回名称为viraj的学生的数据。

Whenever we call a REST service, we generally don't mention the HOST NAME, like in the above example URLs, but hostname is very important to make the URL unique all over the web. The header generally has the hostname in it.

每当我们调用REST服务时,通常都不会像上面的示例URL那样提到主机名,但是主机名对于使URL在整个网络中唯一是非常重要的。 标头中通常包含主机名。

GET students/viraj HTTP/1.1

HOST: studytonight.com

NOTE: While creating a RESTful architecture, keep in mind that resources are thought to be as Nouns, so try to keep it that way. For example, the following is not RESTful:

注意:在创建RESTful架构时,请记住,资源被视为Nouns ,因此请尝试保持这种方式。 例如,以下不是RESTful:

/students/add

You must be wondering that this looks ok, as we can send the data to be added in HTTP request body, but, in RESTful architecture, we do not use URLs to describe actions.

您肯定想知道这样做看起来不错,因为我们可以发送要添加到HTTP请求正文中的数据,但是在RESTful体系结构中,我们不使用URL来描述操作

In REST, /students will be the resource, and the action will be decided based on the HTTP verb used. Based on the HTTP verb, we inform the service, whether to create a new student entry, update the existing one or delete it, using the same resource.

在REST中, /students将是资源,并且将根据所使用的HTTP 动词来决定操作。 基于HTTP动词,我们使用相同的资源通知服务,是创建一个新的学生条目,更新现有的学生条目还是删除它。

HTTP请求:示例起始行 (HTTP Request: Sample Start Line)

GET/hello.htm  HTTP/1.1 (sent by client)

HTTP/1.1 200 OK (sent by server)

HTTP请求:标头字段 (HTTP Request: Header Fields)

Generic Header
    [field-name] : [field-value]

Check the snapshot of Chrome's developers tool above to see sample header.

查看上面的Chrome开发者工具快照,以查看示例标头。

HTTP请求:消息正文 (HTTP Request: Message Body)

The message body is optional and this is the one, which carries the actual HTTP request data from the client and the response data from the server. Here is an example of getting back HTML in response in body.

消息主体是可选的,它是一个主体,它承载来自客户端的实际HTTP请求数据和来自服务器的响应数据。 这是在正文中返回HTML的示例。

<html>
   <body>
      <h1>Welcome</h1>
   </body>
</html>

HTTP方法/动词 (HTTP Methods/Verbs)

The most important part of a request is the HTTP Method (verbs). These methods tells the server what to do with the data received in the request. You must be familiar with GET and POST as they are frequently used in FORM submission in HTML.

请求中最重要的部分是HTTP方法(动词)。 这些方法告诉服务器如何处理请求中接收的数据。 您必须熟悉GETPOST因为它们在HTML表单提交中经常使用。

Although there are 8 different types of methods but the 4 most important HTTP methods will be discussed here which are equivalent to the CRUD operations.

尽管有8种不同类型的方法,但是这里将讨论4种最重要的HTTP方法,它们等效于CRUD操作。

HTTP方法:GET (HTTP Methods: GET)

The GET method is used to get the information from the server. This is equivalent to the SELECT statement in SQL. There is no message body in the GET request as it is meant to only fetch the data from the server.

GET方法用于从服务器获取信息。 这等效于SQL中的SELECT语句。 GET请求中没有消息正文,因为它仅用于从服务器中获取数据。

This is the simplest type, and is used by the browser every time you visit any link, to fetch the data. A GET request should never change the data on server, and should only be used to read data.

这是最简单的类型,每次您访问任何链接时浏览器都会使用它来获取数据。 GET请求不应更改服务器上的数据,而应仅用于读取数据。

GET /students

The above should return the students data.

上面应该返回学生数据。

HTTP方法:POST (HTTP Methods: POST)

This is used to send the data to the server. This can be students information, some XML file or some JPG file. The message body contains the data. POST requests are generally used to create but it can be used to update the existing data as well.

这用于将数据发送到服务器。 这可以是学生信息,一些XML文件或一些JPG文件。 邮件正文包含数据。 POST请求通常用于创建,但也可以用于更新现有数据。

POST /students

The above should be able to create a new student entry, provided student data is sent in appropriate format in the message body. This can be used to update student data as well.

如果在邮件正文中以适当的格式发送了学生数据,则以上内容应该能够创建一个新的学生条目。 这也可以用于更新学生数据。

HTTP方法:PUT (HTTP Methods: PUT)

This should be used when we have to replace/update/create the data, which is already present/not present on the server with the data that we pass in the message body.

当我们必须替换/更新/创建数据时,应使用此属性,该数据已经在我们的消息正文中传递了,而在服务器上已经存在/不存在。

HTTP方法:DELETE (HTTP Methods: DELETE)

This should be used to delete the data on the server. For example, the following URL when sent a DELETE request, should delete the student entry.

这应该用于删除服务器上的数据。 例如,以下URL在发送DELETE请求时,应删除学生条目。

DELETE /students/viraj

You must notice that, with every method, we have mentioned should be used, as we can only advice to follow the standards. Just saying, but DELETE can be used to create a new entry, but we must follow the standards to avoid any confusion.

您必须注意,对于每种方法, 都应使用我们提到的方法,因为我们只能建议您遵循标准。 只是说一句,但是DELETE可用于创建新条目,但是我们必须遵循标准以避免任何混乱。

翻译自: https://www.studytonight.com/rest-web-service/understanding-http

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值