Search code examples
phpapireststatus

REST response code for successful add but failed to return the created object


Okay. I am wondering what is the possible HTTP Status (response) code in this scenario.

Lets say, POST /Student/ creates a student resource. Assume that, there are multiple tables to update. Student resource is created.

Usually, when a resource is created, the resource should be returned immediately in the response with 200 Successful.

However, assume that the resource is created, but when you try to pull the resource (a query from database) fails. What status code should we return?

   `201 Created`? (No resource)
   '200 Successful`? but without the resource. 
   `500 Internal Server Error`?

Something else?

I guess its fairly a common scenario.

EDIT

I want to clarify this. First thing, its all ONE POST call to create a resource. The response should be 200 with the resource object as JSON. But there is an error only during the retrieval of the resource (the resource was created successfully).

The reasons for failure to get the resource form the database might be intermittent and might be server / network related and could be identified and fixed later. But the resource was created for sure successfully.


Solution

  • You could go with 201 Created, as it doesn't mandate to return the created entity. If the client tries to retrieve it, it'll get a 500 Server Error.

    If you want to let the client know of a possible server shenanigan, you could return 202 Accepted, as it is non-committal.

    Whatever you do, I suggest you make this resource idempotent because it seems it has complex failure modes and the client is likely to re-attempt a create.