0

Suppose I have the following resource collection

/api/people

With each resource identified by their IDs

/api/people/1
/api/people/2
...and so on...

Updating is done by sending POST request carrying payload the data to be updated

POST /api/people/20

{"name":"Yan"}

If such update is failing because there's no resource under that ID (in example above is 20) which HTTP status code should be returned.

404 Not Found? Justification being the resource under that URI does not exists.

400 Client Error? Justification being client has failed constructing proper request.

Other statuses?

bluearth
  • 501
  • 4
  • 18

2 Answers2

3

I would go with 404 Not Found since the request can be processed but the resource cannot be found, error 400 indicates the request has a syntax error.

https://developer.mozilla.org/pt-PT/docs/Web/HTTP/Status/400

https://developer.mozilla.org/pt-PT/docs/Web/HTTP/Status/404

In another note if you are updating existing data i think you should use a PUT request.

Diogo Santos
  • 49
  • 1
  • 5
  • Aggre about the method choice. But I'm making provisions for useragents incapable of issuing PUT or intervening proxies that block PUTs – bluearth May 07 '20 at 14:56
0

404 NOT FOUND.

But it's better to use PUT for Updates because it is idempotent. POST is not idempotent and it's design would result better to insert new resources inside certain collection.

So many requests to:

POST /collection results in: collection/1, collection/2, collection/3, ...

PUT /collection/1 results in: collection/1 always

And for successful updates, you can return: 204 No Content

Source: Experience with REST microservices