HTTP 200 响应是否意味着必须有响应 body?
Does a HTTP 200 response imply that there must be a response body?
我正在尝试破译 Swagger 规范文档并使用它来生成代码。它包含以下端点定义:
/feature:
get:
summary: Returns all features
operationId: getAllFeatures
tags:
- feature
responses:
'200':
description: 'Features retrieved successfully'
'400':
$ref: '#/responses/BadRequest'
根据端点摘要和 200 响应描述,我很清楚这个端点旨在 return 包含数组或 collection 的响应 body “功能”,即使响应未在规范中定义。
假设我是对的,规范作者只是忘了添加它。那么我应该如何处理这个端点:
/features:
put:
summary: Updates an existing feature
operationId: updateFeature
parameters:
- name: body
in: body
description: 'Feature to be updated'
required: true
schema:
$ref: '#/definitions/Feature'
tags:
- feature
responses:
'200':
description: 'Feature updated'
这个对我来说是模棱两可的。我已经看到一些 return 更新 object 的更新端点的实现。其他我在 body.
中 return 什么都没有看到
我的问题是:
- HTTP 200 响应是否意味着 必须 是响应 body?我无法判断 HTTP 规范 (https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html) 是否需要 还是它只是声明它 可以 存在。
- 在这种情况下,规范作者使用 HTTP 204 明确表示没有响应是否会减少混淆 body? (我可以回答这个 - 是 - 但在这种情况下是否有理由使用 HTTP 200 而不是 204?或者这只是作者不知道成功响应代码,除了HTTP 200?)
- 如果#1 的答案是否定的,而#2 的答案是肯定的:为什么 HTTP 以这种方式定义?
200 OK
可以 return 一个带有 Content-Length: 0
. 的空体
204 No Content
比许多人意识到的更具体。引自 current spec:
The 204 response allows a server to indicate that the action has been
successfully applied to the target resource, while implying that the
user agent does not need to traverse away from its current "document
view" (if any). The server assumes that the user agent will provide
some indication of the success to its user, in accord with its own
interface, and apply any new or updated metadata in the response to
its active representation.
基本上这就是说,例如,如果提交了 HTML 表单,并且服务器响应 204
,它可以向浏览器发出信号 not 将当前页面刷新到新位置,或重定向到其他任何地方。例如,它可以在不强制浏览器 redirect/switch 到新的 url 的情况下促进 'save' 操作。另请参阅 205
了解类似的操作,但行为不同。
浏览器(据我所知)实际上并没有实现这种行为。但是 REST/Hypermedia/HATEOAS 客户 可以.
当前的规范也说明了更常见的用途,即 200 without a response body
,但如果你一路回到 HTTP/1.0 规范,这就是整个 section.请注意,它 仅 提到了此行为,而没有提及 204
只是 200 minus body:
的替代品
The server has fulfilled the request but there is no new information to send back. If the client is a user agent, it should not change its document view from that which caused the request to be generated. This response is primarily intended to allow input for scripts or other actions to take place without causing a change to the user agent's active document view. The response may include new metainformation in the form of entity headers, which should apply to the document currently in the user agent's active view.
所以这里的关键是,这表明超媒体客户端应该如何表现。删除它,我同意没有太多理由使用 204
。它已成为一种惯例,我认为它没有强烈的目的。
旁注:除非您对互联网考古感兴趣,否则请勿参考 RFC2616。
- 参见#2
我正在尝试破译 Swagger 规范文档并使用它来生成代码。它包含以下端点定义:
/feature:
get:
summary: Returns all features
operationId: getAllFeatures
tags:
- feature
responses:
'200':
description: 'Features retrieved successfully'
'400':
$ref: '#/responses/BadRequest'
根据端点摘要和 200 响应描述,我很清楚这个端点旨在 return 包含数组或 collection 的响应 body “功能”,即使响应未在规范中定义。
假设我是对的,规范作者只是忘了添加它。那么我应该如何处理这个端点:
/features:
put:
summary: Updates an existing feature
operationId: updateFeature
parameters:
- name: body
in: body
description: 'Feature to be updated'
required: true
schema:
$ref: '#/definitions/Feature'
tags:
- feature
responses:
'200':
description: 'Feature updated'
这个对我来说是模棱两可的。我已经看到一些 return 更新 object 的更新端点的实现。其他我在 body.
中 return 什么都没有看到我的问题是:
- HTTP 200 响应是否意味着 必须 是响应 body?我无法判断 HTTP 规范 (https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html) 是否需要 还是它只是声明它 可以 存在。
- 在这种情况下,规范作者使用 HTTP 204 明确表示没有响应是否会减少混淆 body? (我可以回答这个 - 是 - 但在这种情况下是否有理由使用 HTTP 200 而不是 204?或者这只是作者不知道成功响应代码,除了HTTP 200?)
- 如果#1 的答案是否定的,而#2 的答案是肯定的:为什么 HTTP 以这种方式定义?
200 OK
可以 return 一个带有Content-Length: 0
. 的空体
204 No Content
比许多人意识到的更具体。引自 current spec:
The 204 response allows a server to indicate that the action has been successfully applied to the target resource, while implying that the user agent does not need to traverse away from its current "document view" (if any). The server assumes that the user agent will provide some indication of the success to its user, in accord with its own interface, and apply any new or updated metadata in the response to its active representation.
基本上这就是说,例如,如果提交了 HTML 表单,并且服务器响应 204
,它可以向浏览器发出信号 not 将当前页面刷新到新位置,或重定向到其他任何地方。例如,它可以在不强制浏览器 redirect/switch 到新的 url 的情况下促进 'save' 操作。另请参阅 205
了解类似的操作,但行为不同。
浏览器(据我所知)实际上并没有实现这种行为。但是 REST/Hypermedia/HATEOAS 客户 可以.
当前的规范也说明了更常见的用途,即 200 without a response body
,但如果你一路回到 HTTP/1.0 规范,这就是整个 section.请注意,它 仅 提到了此行为,而没有提及 204
只是 200 minus body:
The server has fulfilled the request but there is no new information to send back. If the client is a user agent, it should not change its document view from that which caused the request to be generated. This response is primarily intended to allow input for scripts or other actions to take place without causing a change to the user agent's active document view. The response may include new metainformation in the form of entity headers, which should apply to the document currently in the user agent's active view.
所以这里的关键是,这表明超媒体客户端应该如何表现。删除它,我同意没有太多理由使用 204
。它已成为一种惯例,我认为它没有强烈的目的。
旁注:除非您对互联网考古感兴趣,否则请勿参考 RFC2616。
- 参见#2