0

Question

I want to know what are the best practices (if any) when handling a restful POST where the PARENT entity has CHILD entities that must exist before saving the main entity.

My Thoughts

My initial thoughts are 404s are for when the PARENT entity is NOT FOUND. However, when the CHILD entities are not found a 422 should be returned.

Why a 422? Well IMO a 422 is the server saying, "Hey I found the main entity you were after. I know what you are trying to do (create this parent entity). However, I cannot find all the information I need to finish creating the PARENT entity."

Case Study

Below I've created some sample data and schemas to help illustrate the question.

Case 1

Sending the following should succeed with 200 if "Spaghetti" and "BestBuy" exist in the foreign tables.

POST /people
{
    firstName: "John",
    lastName: "Doe",
    favoriteFoodName: "Spaghetti",
    favoriteStoreName: "BestBuy"
}
Case 2

Sending the following should succeed with 422 if either "Sushi" or "Old Navy" don't exist in the foreign tables.

POST /people
{
    firstName: "Jane",
    lastName: "Doe",
    favoriteFoodName: "Sushi",
    favoriteStoreName: "Old Navy"
}

Data Models

enter image description here

Michael
  • 308
  • 3
  • 12
  • `422` seems to be the right return code for your `child entity failed` condition. For further details, you can check https://stackoverflow.com/questions/42135803/what-is-the-correct-http-status-code-for-a-child-entity-that-is-not-found. – YoManTaMero Jun 11 '21 at 04:28

1 Answers1

0

409 is probably the best status. 409 tells a client, "this request would be valid if the state of the resource or other resources were different".

So if the 'children' still need to be created, and after which the original request can be repeated and will be successful, I think 409 is right.

Evert
  • 93,428
  • 18
  • 118
  • 189