gpt4 book ai didi

java - 当没有创建资源时,我应该为 POST 返回什么 HTTP 状态代码?

转载 作者:可可西里 更新时间:2023-11-01 15:15:48 25 4
gpt4 key购买 nike

我正在将图像发布到我的服务器(使用 Java 和 Spring)。

可能会发生一些事情:

  • 如果一切顺利,我将返回 200 和图像 ID。
  • 无法将图像保存到文件系统(SaveFileException)。
  • 数据库无法使用新 URL 更新新图像记录(DatabaseException)。
  • 或者抛出一个IOException

简而言之,我无法保存/创建图像。

我应该返回什么 HTTP 代码?

根据Exception是否应该返回多条代码和多条消息?

最佳答案

首先,让我强调一下,状态码是为了指示服务器尝试理解和满足客户端请求的结果。

If everything goes well, then I return 200 and the image id.

似乎没问题,但我建议您返回 201 以及 Location header 代替。引用 RFC 7231关于 POST 方法:

If one or more resources has been created on the origin server as a result of successfully processing a POST request, the origin server SHOULD send a 201 (Created) response containing a Location header field that provides an identifier for the primary resource created and a representation that describes the status of the request while referring to the new resource(s).

201 一起,Location header 用于指示新创建的资源所在的位置。如果未提供 Location header ,则客户端应假定该资源由有效请求 URI 标识:

6.3.2. 201 Created

The 201 (Created) status code indicates that the request has been fulfilled and has resulted in one or more new resources being created. The primary resource created by the request is identified by either a Location header field in the response or, if no Location field is received, by the effective request URI. [...]


客户端错误

客户端能否执行新请求并解决问题?如果是这样,请在 4xx 范围内选择一个状态代码:

6.5. Client Error 4xx

The 4xx (Client Error) class of status code indicates that the client seems to have erred. Except when responding to a HEAD request, the server SHOULD send a representation containing an explanation of the error situation, and whether it is a temporary or permanent condition. These status codes are applicable to any request method.

Michael Kropat放在一起很有用set of flowcharts这可能会给你一些见解。请参阅下表以确定最合适的 4xx 状态代码:

Choosing a 4xx status code

根据导致错误的原因,一些有效的选项是:

6.5.11. 413 Payload Too Large

The 413 (Payload Too Large) status code indicates that the server is refusing to process a request because the request payload is larger than the server is willing or able to process. [...]

6.5.13. 415 Unsupported Media Type

The 415 (Unsupported Media Type) status code indicates that the origin server is refusing to service the request because the payload is in a format not supported by this method on the target resource. The format problem might be due to the request's indicated Content-Type or Content-Encoding, or as a result of inspecting the data directly.

6.5.1. 400 Bad Request

The 400 (Bad Request) status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).


服务器错误

如果错误是由服务器引起的,则 5xx 范围内的状态代码将是准确的:

6.6. Server Error 5xx

The 5xx (Server Error) class of status code indicates that the server is aware that it has erred or is incapable of performing the requested method. Except when responding to a HEAD request, the server SHOULD send a representation containing an explanation of the error situation, and whether it is a temporary or permanent condition.

请看下面的流程图:

Choosing a 5xx status code

我会建议 500 :

6.6.1. 500 Internal Server Error

The 500 (Internal Server Error) status code indicates that the server encountered an unexpected condition that prevented it from fulfilling the request.

关于java - 当没有创建资源时,我应该为 POST 返回什么 HTTP 状态代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55685576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com