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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Aggre about the method choice. But I'm making provisions for useragents incapable of issuing PUT or intervening proxies that block PUTs
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.