简体   繁体   中英

Http status code return

如果用户的发帖请求正确但数据库中没有任何更新,因为用户正在为数据库中已经存在的每个字段发送相同的值,应该向用户发送什么HTTP状态代码?

A 200 status would definitely be perfectly appropriate in this case.

What you are describing is usually something that an application on top of the HTTP-based API would handle/add as context.

One lesser-known status code which could be used in such cases, however, is 204.

"The 204 (No Content) status code indicates that the server has successfully fulfilled the request and that there is no additional content to return in the response payload body"

In other words, depending on your application's setup, you could use a 204 (with no response body) to indicate that the PUT/update request itself was successful. but that nothing was modified.

See here for further reading on 204: http://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-19#section-7.2.5

You will use 204 in this case.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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