return 清空正文是个好习惯吗?
Is it a good practice to return empty body?
这可能不是编码问题,但应该足够相关。
我在 Rails 上使用 Ruby 并在 API 上使用它。
我只是设置了 header[:Status] = "200" 并且没有正文内容。
但最近在 heroku 上我注意到如果我尝试访问它,我会进入网页:
This page contains the following errors:
error on line 1 at column 1: Document is empty
Below is a rendering of the page up to the first error.
API 仍然有效,但我想知道这是否是一种好的做法。我主要是 return 没有正文内容所以 API 可以更快。但是如果它需要渲染这个错误页面,那么我不确定什么是最好的。
如果你想return一个没有body的“成功”,there's a status code for that:
10.2.5 204 No Content
The server has fulfilled the request but does not need to return an entity-body, and might want to return updated metainformation. The response MAY include new or updated metainformation in the form of entity-headers, which if present SHOULD be associated with the requested variant.
If the client is a user agent, it SHOULD NOT change its document view from that which caused the request to be sent. This response is primarily intended to allow input for actions to take place without causing a change to the user agent's active document view, although any new or updated metainformation SHOULD be applied to the document currently in the user agent's active view.
The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields.
这可能不是编码问题,但应该足够相关。 我在 Rails 上使用 Ruby 并在 API 上使用它。 我只是设置了 header[:Status] = "200" 并且没有正文内容。
但最近在 heroku 上我注意到如果我尝试访问它,我会进入网页:
This page contains the following errors:
error on line 1 at column 1: Document is empty
Below is a rendering of the page up to the first error.
API 仍然有效,但我想知道这是否是一种好的做法。我主要是 return 没有正文内容所以 API 可以更快。但是如果它需要渲染这个错误页面,那么我不确定什么是最好的。
如果你想return一个没有body的“成功”,there's a status code for that:
10.2.5 204 No Content
The server has fulfilled the request but does not need to return an entity-body, and might want to return updated metainformation. The response MAY include new or updated metainformation in the form of entity-headers, which if present SHOULD be associated with the requested variant.
If the client is a user agent, it SHOULD NOT change its document view from that which caused the request to be sent. This response is primarily intended to allow input for actions to take place without causing a change to the user agent's active document view, although any new or updated metainformation SHOULD be applied to the document currently in the user agent's active view.
The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields.