简体   繁体   English

了解REST响应和HTTP状态代码

[英]understanding REST Response and HTTP status code

I wanted to know how I should respond in my REST API. 我想知道我应该如何在我的REST API中响应。

Valid Example: 有效示例:

http://blah.com/api/v1/dosomething/123

The above is a valid request and currently I have a HTTP Status of 200 with a JSON response 以上是有效的请求,目前我的HTTP状态为200,带有JSON响应

{
    "dosomething": {
        "status": "OK",
        "results": "123"
    }
}

Now my question is, if the parameter passed is not valid ( I'm expecting a string of whole numbers ), do I return a HTTP Response of 200 and pass the error status back in the JSON response or should I pass something like a HTTP 400 response ( Bad request ) and list the error / issue with the request in the JSON response? 现在我的问题是,如果传递的参数无效(我希望是一串整数),我是否返回200的HTTP响应并将错误状态传递回JSON响应中,还是应该传递类似HTTP的信息? 400响应(错误请求),并在JSON响应中列出请求的错误/问题?

Error Example: 错误示例:

http://blah.com/api/v1/dosomething/123a

JSON Response: JSON响应:

{
    "dosomething": {
        "status": "ERROR",
        "errors": [
            "Value passed: |123a| must be a integer."
        ]
    }
}

Again my question is should I pass a 200 or 400 HTTP status on the request where the parameter passed is not what I'm expecting? 同样,我的问题是我应该在请求传递的参数不是我期望的值的情况下传递200或400 HTTP状态吗? Or should this always be a 200 response as the request is working? 还是应该始终是200响应,因为请求正在运行?

What is considered best practice? 什么是最佳做法?

Use 404. Always. 使用404。始终。 404. To do otherwise is to misunderstand the nature of a URI and a resource. 404.否则,就是误解了URI和资源的性质。 If http://blah.com/api/v1/dosomething/ identified the resource, and 123a were merely a parameter to it, then other codes could make sense. 如果http://blah.com/api/v1/dosomething/标识了该资源,而123a仅仅是该资源的参数,则其他代码可能有意义。 But it doesn't: http://blah.com/api/v1/dosomething/123 identifies the resource. 但这没有: http://blah.com/api/v1/dosomething/123 : http://blah.com/api/v1/dosomething/123标识资源。 If no such resource exists, return 404 Not Found . 如果不存在这样的资源,则返回404 Not Found

You might possess some implementation detail that handles both resources http://blah.com/api/v1/dosomething/123 and http://blah.com/api/v1/dosomething/123a , but it is not the resource. 您可能拥有一些处理细节,可以同时处理这两个资源http://blah.com/api/v1/dosomething/123http://blah.com/api/v1/dosomething/123a ,但这不是资源。 From Roy Fielding's dissertation : 从罗伊·菲尔丁(Roy Fielding)的论文中

"The resource is not the storage object. The resource is not a mechanism that the server uses to handle the storage object. The resource is a conceptual mapping -- the server receives the identifier (which identifies the mapping) and applies it to its current mapping implementation (usually a combination of collection-specific deep tree traversal and/or hash tables) to find the currently responsible handler implementation and the handler implementation then selects the appropriate action+response based on the request content. All of these implementation-specific issues are hidden behind the Web interface; their nature cannot be assumed by a client that only has access through the Web interface." “资源不是存储对象。资源不是服务器用来处理存储对象的机制。资源是概念性映射-服务器接收标识符(标识映射)并将其应用于当前映射实现(通常是特定于集合的深树遍历和/或哈希表的组合)以找到当前负责的处理程序实现,然后处理程序实现会根据请求内容选择适当的操作+响应。隐藏在Web界面后面;只能通过Web界面访问的客户端不能假设其性质。”

Edit by author: 422 is a wrong answer. 由作者编辑:422是错误的答案。 I misunderstood initial question and gave invalid answer. 我误解了最初的问题并给出了无效的答案。 Please see response by @fumanchu: https://stackoverflow.com/a/10955717/441250 . 请参阅@fumanchu的回复: https ://stackoverflow.com/a/10955717/441250。 My answer below is wrong. 我下面的回答是错误的。

I'd suggest to use "422 Unprocessable Entity" and include failure information in the body of your response. 我建议使用“ 422无法处理的实体”,并在响应的正文中包含失败信息。

The 422 (Unprocessable Entity) status code means the server 422(不可处理实体)状态代码表示服务器
understands the content type of the request entity (hence a 了解请求实体的内容类型(因此
415(Unsupported Media Type) status code is inappropriate), and the 415(不受支持的媒体类型)状态码不正确),并且
syntax of the request entity is correct (thus a 400 (Bad Request) 请求实体的语法正确(因此为400(错误请求)
status code is inappropriate) but was unable to process the contained instructions. 状态代码不正确),但无法处理其中的说明。 For example, this error condition may occur if an XML 例如对于如果XML可能会出现此错误情况
request body contains well-formed (ie, syntactically correct), but 请求主体包含格式正确的(即,语法正确的),但是
semantically erroneous, XML instructions. 语义错误的XML指令。

It's unacceptable to use "200 Ok" or any other status codes when dealing with errors. 处理错误时,使用“ 200 Ok”或任何其他状态代码是不可接受的。

PS List of status codes: http://www.iana.org/assignments/http-status-codes/http-status-codes.xml PS状态代码列表: http : //www.iana.org/assignments/http-status-codes/http-status-codes.xml

HTTP 400 is used to signify a problem with the HTTP request itself (such as an invalid HTTP header). HTTP 400用于表示HTTP请求本身存在问题(例如无效的HTTP标头)。 Although you are not receiving the parameters you expect, the request is still a valid HTTP request, so I would return a 200 response but include details of the missing parameter in your JSON. 尽管您没有收到期望的参数,但该请求仍然是有效的HTTP请求,因此我将返回200响应,但在JSON中包含缺少的参数的详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM