HTTP Get with 204 No Content: Is that normal

后端 未结 5 695
没有蜡笔的小新
没有蜡笔的小新 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:48

    Http GET returning 204 is perfectly fine, and so is returning 404.

    The important thing is that you define the design standards/guidelines for your API, so that all your endpoints use status codes consistently.

    For example:

    • you may indicate that a GET endpoint that returns a collection of resources will return 204 if the collection is empty. In this case GET /complaints/year/2019/month/04 may return 204 if there are no complaints filed in April 2019. This is not an error on the client side, so we return a success status code (204). OTOH, GET /complaints/12345 may return 404 if complaint number 12345 doesn't exist.
    • if your API uses HATEOAS, 204 is probably a bad idea because the response should contain links to navigate to other states.

提交回复
热议问题