2

Let's say I have an endpoint like PATCH /person/<personId> to which I post some JSON like

{ parent: <parentId> }

What would the appropriate HTTP response code be, if the person with id <parentId> is not found? It should clearly be 404 if the person with id <personId> is not found, but does this also apply to resources not directly mentioned in the URI of the request itself?

I've tried looking at some explanations of the HTTP error codes, but none were clear on this point.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
pydora
  • 123
  • 5

1 Answers1

2

You should use 422 to indicate that the given entity cannot be processed by the server. From the RFC 5789, the document the defines the PATCH method:

Unprocessable request: Can be specified with a 422 (Unprocessable Entity) response when the server understands the patch document and the syntax of the patch document appears to be valid, but the server is incapable of processing the request. This might include attempts to modify a resource in a way that would cause the resource to become invalid; for instance, a modification to a well-formed XML document that would cause it to no longer be well-formed. There may also be more specific errors like "Conflicting State" that could be signaled with this status code, but the more specific error would generally be more helpful.


Solutions to address similar situations have been previously described here and here.

Community
  • 1
  • 1
cassiomolin
  • 124,154
  • 35
  • 280
  • 359