HTTP Get with 204 No Content: Is that normal

后端 未结 5 733
没有蜡笔的小新
没有蜡笔的小新 2021-01-30 10:11

Is it a normal occurrence for an HTTP GET Request to have a response with status code 204 - No Content? Like, is this semantically correct concerni

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-30 10:37

    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.

    According to the RFC part for the status code 204, it seems to me a valid choice for a GET request.

    A 404 Not Found, 200 OK with empty body and 204 No Content have completely different meaning, sometimes we can't use proper status code but bend the rules and they will come back to bite you one day or later. So, if you can use proper status code, use it!

    I think the choice of GET or POST is very personal as both of them will do the work but I would recommend you to keep a POST instead of a GET, for two reasons:

    • You want the other part (the servlet if I understand correctly) to perform an action not retrieve some data from it.
    • By default GET requests are cacheable if there are no parameters present in the URL, a POST is not.

提交回复
热议问题