gpt4 book ai didi

RESTful POST 响应

转载 作者:行者123 更新时间:2023-12-05 01:17:43 28 4
gpt4 key购买 nike

我有一个非常简单的 RESTful 服务,它通过 POST 接收一些表单数据,其目的是在云存储(Amazon S3、Azure Blob 存储等)中简单地保留文本主体(具有唯一 ID)作为一个文件...

所以,问题是......如果一切都很好,并且我向调用者返回 200 响应,我应该返回给调用者的正文是什么?

什么?什么都没有?

如果这是创建一个数据库记录..也许该新记录的 id 可能有用..但在这种情况下,我认为只是 HttpRepsone 代码就足够了吗?

有人同意、不同意或有讨论的支持链接吗?

我应该补充一点,对这个服务的请求几乎是......交出,然后去......他们实际上并不需要我提议发回的唯一ID......这个更多的是为了“完整性”。

最佳答案

简答

一旦您在服务器上创建资源,您应该返回 201状态代码以及 Location header ,允许客户端定位新创建的资源。

响应负载是可选的,它通常描述并链接到创建的资源。

稍微长一点的回答

请参阅 RFC 7231 中的以下引述:

4.3.3. 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). [...]

同样来自 RFC 7231 :

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.

The 201 response payload typically describes and links to the resource(s) created. [...]

7.1.2. Location

The Location header field is used in some responses to refer to a specific resource in relation to the response. The type of relationship is defined by the combination of request method and status code semantics.

Location = URI-reference

The field value consists of a single URI-reference. [...] For 201 (Created) responses, the Location value refers to the primary resource created by the request. [...]

关于RESTful POST 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49054408/

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