rest api_了解REST API响应

本文介绍了REST API响应的格式,如XML和JSON,以及如何根据HTTP请求的Media-Type属性调整响应。同时,详细讨论了HTTP响应的状态码,包括1XX信息代码、2XX成功代码、3XX重定向、4XX客户端错误和5XX服务器错误,以及这些状态码在实际应用中的意义。文章以学生注册API为例,说明了在不同场景下如何使用合适的状态码。
摘要由CSDN通过智能技术生成

rest api

As the REST API's response is consumed by some application and not the browser, so we don't have to worry about styling it to make it look good.

由于REST API的响应是由某些应用程序而不是浏览器消耗的,因此我们不必担心对其进行样式设置以使其看起来不错。

In case of API response, it can be simple XML or JSON or any other media type. Also, at times, one REST API is being consumed by different applications. Out of which, one application might need the response in the form of XML and other might need the response in the form of JSON. We can develop such REST API, programming them to send the response according to the input header of the HTTP request.

对于API响应,它可以是简单的XMLJSON或任何其他媒体类型。 而且,有时,一个REST API被不同的应用程序使用。 其中,一个应用程序可能需要XML形式的响应,而其他应用程序可能需要JSON形式的响应。 我们可以开发这样的REST API,对它们进行编程以根据HTTP请求的输入标头发送响应。

There is a Media-Type attribute in the header which can be used in such cases and the response can be sent accordingly. We will learn how to do this in coming tutorials but for now lets concentrate on the design aspects of the REST API.

标头中有一个Media-Type属性,在这种情况下可以使用该属性,并且可以相应地发送响应。 我们将在接下来的教程中学习如何执行此操作,但是现在让我们集中精力于REST API的设计方面。

If we talk about the Student record, in our Student-Courses Application, the response will be something like this,

如果我们谈论学生记录,在我们的学生课程应用程序中,响应将是这样,

The Student class (Data Model/Object) will look something like this (using Java):

Student类(数据模型/对象)将类似于以下内容(使用Java):

public class Student{
    private int rollno;
    private String firstName;
    private String LastName;
    private int age;
    private String contactNumber;
    ...
}

The XML response will be something like this:

XML响应将如下所示:

<Student>
    <rollno>10</rollno>
    <firstName>Amit</firstName >
    <lastName>Agarwal</lastName>
    <age>25</age>
</Student>

The JSON response will be something like this:

JSON响应将如下所示:

{
    "rollno":"10",
    "firstName":"Amit",
    "lastName":"Agarwal",
    "age":"25"
    "contactNumber":"98877271127"
}

The above response is not the complete response its just the response body. An HTTP response contains the status line, headers and the message/response body.

以上响应不是完整响应,而仅仅是响应主体 。 HTTP响应包含状态行标头消息/响应主体

As we have already discussed that same REST API can return both XML or JSON as response message, depending upon the Media-Type attribute in the HTTP request.

正如我们已经讨论的那样,相同的REST API可以返回XMLJSON作为响应消息,这取决于HTTP请求中的Media-Type属性。

The question here is, how will the client know, what type of response to expect from the API. This is again managed by the response headers. There is a Content Type attribute in the headers which informs about the type of response body format.

这里的问题是, 客户将如何知道该API期望什么样的响应。 再次由响应头进行管理。 标头中有一个Content Type属性,用于通知响应主体格式的类型。

响应(状态)代码 (Response(Status) Codes)

Till now, we have discussed about the situations where everything is good but this doesn't happen in the real world scenario. You may have seen many times, that a particular website is down or not functioning properly. The same can also happen to the REST Api which you have developed and deployed on the server.

到现在为止,我们已经讨论了一切都很好的情况,但是在现实世界中这不会发生。 您可能已经多次看到某个特定的网站已关闭或无法正常运行。 您在服务器上开发和部署的REST Api也可能发生同样的情况。

There can be many scenarios, like:

可能有很多情况,例如:

  1. The server on which you have deployed the Rest API is down.

    您已在其上部署Rest API的服务器已关闭。

  2. The client request is incorrect.

    客户端请求不正确。

  3. The resource which the client is requesting doesn't exist.

    客户端请求的资源不存在。

  4. Some error occured on the server side while processing the request.

    处理请求时,服务器端发生了一些错误。

So the REST API must always return an appropriate status code to the client so that the client can know the actual issue and process accordingly.

因此,REST API必须始终将适当的状态代码返回给客户端,以便客户端可以知道实际的问题并进行相应的处理。

Below mentioned are the standard status codes, used in HTTP responses.

下面提到的是HTTP响应中使用的标准状态代码。

1XX代码:信息代码 (1XX Codes: Informational Codes)

These are the acknowledgement responses, used to pass on information. This status code is not used commonly.

这些是用于传递信息的确认响应。 此状态代码不常用。

2XX代码:成功代码 (2XX Codes: Success Codes)

These are the success codes which means that the server has received the request from the client and processed it successfully.

这些是成功代码,表示服务器已从客户端收到请求并成功处理了该请求。

For Example:

例如:

  • 200 OK: This informs the client about the successful response.

    200 OK:这将通知客户端有关成功的响应。

  • 201 Created: This should be returned for POST requests, stating that the resource is created successfully on the server. Like, when the client sends: POST /myApp/students/ request and the student recourse is created successfully then the server should return 201 Created as status code. The server can also return 200 OK, but it's always good to be more precise, and send 201 Created in case of a POST request.

    201已创建:应该为POST请求返回此消息,表明已在服务器上成功创建资源。 例如,当客户端发送: POST / myApp / students /请求并且成功创建了学生资源后,服务器应返回201 Created作为状态码。 服务器也可以返回200 OK ,但是更精确一点总是好事,如果有POST请求,则发送201 Created。

  • 202 Accepted: This informs the client that the request has been successfully received, but the processing is not yet finished.

    202 Accepted:这通知客户端已成功接收到请求,但处理尚未完成。

  • 204 No Content: This informs the client that the request has been successfully processed, but no content will be returned.

    204 No Content:通知客户端请求已成功处理,但不返回任何内容。

  • Check this for the complete list of 2XX status codes: 2XX Status Codes

    检查此列表以获取2XX状态代码的完整列表: 2XX状态代码

3XX代码: (3XX Codes:)

These codes are generally used in case of URL Redirection.

这些代码通常在URL重定向的情况下使用。

4XX代码: (4XX Codes:)

This class of status codes are returned if the client's request has error. The request could be incorrect or the resource which the client is looking for doesn't exist.

如果客户端的请求有错误,则返回此类状态码。 该请求可能不正确,或者客户端正在寻找的资源不存在。

  • 400 Bad Request: There is something wrong in the request from the client, hence the server cannot or will not process it.

    400错误的请求:来自客户端的请求有问题,因此服务器无法或将不处理该请求。

  • 401 Unauthorized: The client needs to authorize themselves to make this request.

    401未经授权:客户需要授权自己才能提出此请求。

  • 403 Forbidden: This status code is used when the client request is correct but the server refuses to process the request. The client might not have required permissions.

    403禁止访问:客户端请求正确但服务器拒绝处理该请求时,将使用此状态代码。 客户端可能没有所需的权限。

  • 404 Not Found: The resource which the client is requesting for doesn't exist.

    找不到404:客户端请求的资源不存在。

5XX代码:服务器错误 (5XX Codes: Server error)

The 5XX status codes are when server fails to process the request and cannot send the correct response. In this case the client doesn't have to change its request as the problem is with the server where the REST API is deployed.

5XX状态代码是服务器无法处理请求并且无法发送正确响应时的状态代码。 在这种情况下,客户端不必更改其请求,因为存在REST API的服务器存在问题。

  • 500 Internal Server Error: This is a generic status code returned when an unexpected condition is encountered on the server, while processing the request.

    500内部服务器错误:这是在处理请求时服务器上遇到意外情况时返回的通用状态代码。

  • 503 Service Unavailable: When server is not available due to excessive load or may be down for maintainence, this status code is returned to the client.

    503服务不可用:由于负载过大导致服务器不可用或由于维护而停机时,此状态代码将返回给客户端。

状态码和我们的注册API (Status Codes and our Registration API)

In case of our Registration REST Api, we will have to send various Status Codes, whenever a request is received.

对于我们的注册REST Api,无论何时收到请求,我们都必须发送各种状态代码。

Lets summarize the test cases and their probable responses.

让我们总结一下测试用例及其可能的响应。

OperationURIMethodSuccess/FailureStatus Code
GET student recordmyApp/students/{rollNo}GETSuccess200
Not Found404
Failure500
DELETE student recordmyApp/students/{rollNo}DELETESuccess200 or 204
Not Found404
Failure500
UPDATE student recordmyApp/students/{rollNo}PUTSuccess200
Wrong Format/Data400 or 415
Not Found404
Failure500
CREATE student recordmyApp/students/{rollNo}POSTSuccess201
Wrong Format/Data400 or 415
Failure500
操作方式 URI 方法 成功/失败 状态码
获取学生记录 myApp/students/{rollNo} 得到 成功 200
未找到 404
失败 500
删除学生记录 myApp/students/{rollNo} 删除 成功 200或204
未找到 404
失败 500
更新学生记录 myApp/students/{rollNo} 成功 200
格式/数据错误 400或415
未找到 404
失败 500
创建学生记录 myApp/students/{rollNo} 开机自检 成功 201
格式/数据错误 400或415
失败 500

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

rest api

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值