HTTP GET Request Status 204 Vs 404

后端 未结 5 783
北海茫月
北海茫月 2020-12-25 11:06

I have 2 resources User and Album. An user has a list of albums. To get albums there are 2 REST API.

  1. user/{userId}/albums/{albumId} get album by albumId if not
5条回答
  •  长发绾君心
    2020-12-25 11:55

    When you ask for a specific resource, say a user, and the user doesn't exist, then you should return 404. For example, you have an API to retrieve a user using the following URL:

    https://yourdomain.com/api/users/:userid
    

    and a request is made to retrieve user 1234, that doesn't exist, then you should return 404. In this case, the client requested a resource that doesn't exist.

    https://yourdomain.com/api/users/1234 
    404
    

    Now suppose you have an api that returns all users in the system using the following url:

    https://yourdomain.com/api/users
    

    If there are no users in the system, then, in this case, you should return 204.

提交回复
热议问题