得到响应头和服务器响应时间_宁静的api和服务器响应

得到响应头和服务器响应时间

介绍 (Introduction)

APIs are software developers’ best friend. APIs, or application programming interfaces, help define how communication between different types of softwares should take place, and without them creating any sort of meaningful application would be virtually impossible. RESTful APIs, defined by Roy Fielding in his dissertation, take this importance a step further, as they define methods for applications to make HTTP requests to access data from servers. In this article, we’ll look at some of the inner-workings of RESTful APIs, including how servers respond upon receiving HTTP requests.

API是软件开发人员的最好朋友。 API或应用程序编程接口可帮助定义不同类型的软件之间的通信方式,如果没有它们就无法创建任何有意义的应用程序。 Roy Fielding在其论文中定义的RESTful API将这一重要性进一步提高了,因为它们定义了应用程序发出HTTP请求以从服务器访问数据的方法。 在本文中,我们将研究RESTful API的一些内部工作原理,包括服务器如何在收到HTTP请求时做出响应。

RESTful API (RESTful APIs)

One important aspect of REST APIs and http requests are that the client and server must remain separate from one another. Users should not have any interaction with how data is stored behind the scenes, and this in turn also allows for the creation of better user interfaces. Another important part of RESTful APIs is that client requests should not rely on any information or state that is stored on the server to complete the request, but rather must contain all of the necessary information to complete the request in its body. The concept of idempotence is that some operations will always result in the same outcome. We can connect this with client requests because a duplicate client GET request to a server should always contain the same response. Because HTTP requests should not use any additional data from the server to be completed, this idea of idempotence holds true for many interactions we have between clients and servers. What exactly happens, though, when the server actually receives the request?

REST API和http请求的一个重要方面是,客户端和服务器必须保持彼此独立。 用户不应与后台存储数据的方式发生任何交互,而这又可以创建更好的用户界面。 RESTful API的另一个重要部分是,客户端请求不应依赖于服务器上存储的任何信息或状态来完成请求,而必须包含所有必要信息以在其主体中完成请求。 幂等的概念是某些操作将始终导致相同的结果。 我们可以将其与客户端请求连接,因为对服务器的重复客户端GET请求应始终包含相同的响应。 因为HTTP请求不应使用来自服务器的任何其他数据来完成,所以幂等的思想对于客户端和服务器之间的许多交互都适用。 但是,当服务器实际收到请求时,究竟发生了什么?

HTTP请求 (HTTP Requests)

When a server receives an HTTP request, there are several steps that happen. Servers are expecting data to arrive in a specific manner, and if a request does not contain specific information it will send an error back to the user. Common errors include 40x errors (including the dreaded 404 error that most people have likely encountered before), and 400 level errors are returned when there is something wrong with the client’s request.

服务器收到HTTP请求时,将发生多个步骤。 服务器期望数据以特定的方式到达,如果请求不包含特定的信息,它将把错误发送回用户。 常见错误包括40x错误(包括大多数人以前可能遇到的可怕的404错误),并且当客户的请求有问题时,将返回400级错误。

Image for post

We can think of HTTP requests working similarly to how an online library system functions. It is possible to create searches that access the library’s database and return all books by a certain author or keyword. This is analogous to a GET request. We can request a book to check out, which will send information to the library, and is similar to a POST. And, if we’ve decided that we have chosen too many books to read, we can remove a book from our checkout list, much like a DELETE request would work.

我们可以想到HTTP请求的工作方式类似于在线图书馆系统的功能。 可以创建访问图书馆数据库并通过特定作者或关键字返回所有书籍的搜索。 这类似于GET请求。 我们可以要求将一本书检出,这会将信息发送到图书馆,并且类似于POST。 而且,如果我们决定选择过多的书籍以供阅读,则可以从结帐清单中删除一本书,就像删除请求一样。

What were to happen if we were requesting a book to checkout, but in the time that we were reading the book’s description, another user had reserved that same book? Let’s dive under the hood and explore how servers react to requests. In general, 200 level HTTP status codes represent successful requests, 300 level codes represent redirection, 400 level errors represent something wrong with the client request, and 500 level errors represent something wrong with the server.

如果我们要求一本书结帐,但是在阅读本书的描述时,另一位用户保留了同一本书,该怎么办? 让我们深入探讨服务器如何响应请求。 通常,200个级别的HTTP状态代码表示成功的请求,300个级别的代码表示重定向,400个级别的错误表示客户端请求出错,而500个级别的错误表示服务器出错。

Image for post

Upon a successful search for a book in our online library website, we would likely get back a 200 OK message. Creating new resources in the server, such as when successfully completing a hold for a book, yields a 201 Created message. A common 300 level message is 301 Moved Permanently. This could happen if the library had a resource in the past, but the book was moved to a different branch.

在我们的在线图书馆网站上成功搜索一本书后,我们很可能会收到200 OK消息。 在服务器中创建新资源(例如成功完成书籍的保留时)会产生201 Created消息。 常见的300级消息是301永久移动 。 如果图书馆过去有资源,但是这本书被移到了另一个分支,则可能发生这种情况。

400 level errors are a bit wider in scope. 401 Unauthorized messages result when authorization credentials are not present. Imagine not being logged in as a user but still requesting a book from the library. No bueno! 403 Forbidden requests happen when a user is authenticated but does not have permission for the particular resource. Think Harry Potter being a Hogwarts student and being able to access the library, but not any just book he pleased located in the restricted section (without permission from a professor). 404 Not Found errors happen when requesting unavailable resources. We can think back to our example here of requesting a book that has already been checked out, or books that were returned damaged and are currently being restored or repurchased and not currently available.

400级错误的范围更广。 当不存在授权凭证时,将产生401未经授权的消息。 想象一下,不是以用户身份登录,而是仍在向图书馆索要一本书。 没有布宜诺斯艾利斯! 403禁止请求在用户通过身份验证但没有特定资源的权限时发生。 认为哈利·波特(Harry Potter)是霍格沃茨(Hogwarts)的学生,并且可以访问图书馆,但不能随意浏览受限区域中的任何书(未经教授许可)。 请求不可用的资源时,会出现404 Not Found错误。 我们可以回想一下此处的示例,该示例请求已检出的书或退回的书损坏且正在恢复或购回且当前不可用的书。

Image for post
A request to the Youtube API will yield a 403 error without a valid API key.
如果没有有效的API密钥,对Youtube API的请求将产生403错误。

结论 (Conclusion)

RESTful APIs are particularly useful to developers because they explain how to make requests for resources using HTTP methods, which is a vital piece of today’s computing. When servers receive client requests, they must interpret the request and try to access data to return to the client, or update or add information to the database if the client is making a POST or DELETE request. Servers then determine if the user has proper access to materials and then responds to requests accordingly.

RESTful API对开发人员特别有用,因为它们说明了如何使用HTTP方法来请求资源,这是当今计算的重要组成部分。 当服务器接收到客户端请求时,它们必须解释该请求,并尝试访问数据以返回到客户端,或者如果客户端正在发出POST或DELETE请求,则将信息更新或添加到数据库中。 然后,服务器确定用户是否可以正确访问资料,然后相应地响应请求。

翻译自: https://medium.com/@gregthompson27/restful-apis-and-server-responses-2411a6aa21a3

得到响应头和服务器响应时间

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值