REST API 404: Bad URI, or Missing Resource?

前端 未结 9 2170
感动是毒
感动是毒 2020-12-04 05:40

I\'m building a REST API, but I\'ve encountered a problem.

It seems that accepted practice in designing a REST API is that if the resource requested doesn\'t exist,

9条回答
  •  难免孤独
    2020-12-04 05:53

    404 Not Found technically means that uri does not currently map to a resource. In your example, I interpret a request to http://mywebsite/api/user/13 that returns a 404 to imply that this url was never mapped to a resource. To the client, that should be the end of conversation.

    To address concerns with ambiguity, you can enhance your API by providing other response codes. For example, suppose you want to allow clients to issue GET requests the url http://mywebsite/api/user/13, you want to communicate that clients should use the canonical url http://mywebsite/restapi/user/13. In that case, you may want to consider issuing a permanent redirect by returning a 301 Moved Permanently and supply the canonical url in the Location header of the response. This tells the client that for future requests they should use the canonical url.

提交回复
热议问题