Reputation: 43
I have a Restfull API which manages products. I already have a POST request to create a product and I would like to add a POST endpoint (postByCode) with a barcode in the body. This new endpoint will use another API to create the product by retrieving the informations from it.
First question, what kind of HTTP status shall I use in case of the external API doesn't know the barcode (currently I return a 404 which is weird for a POST) ?
Sometimes, the exernal API knows the barcode but doesn't have the needed informations so I need to return the partial product for the user to update missing informations. Second question, is it clean to return the partial product in my postByCode with a 307 HTTP Status ?
I'm thinking about replacing my postByCode by a getByCode and use the standart POST endpoint but it implies to do 2 calls for each product so a clean way to do it with a single request would be great.
Thank you in advance :)
Upvotes: 0
Views: 149
Reputation: 57214
First question, what kind of HTTP status shall I use in case of the external API doesn't know the barcode (currently I return a 404 which is weird for a POST) ?
Keep in mind that you've got the message-body of the response to describe the problem in detail to the client (think of how we would handle this on the web - the response would be an HTML document that the browser could render, so that the human can read that there is a problem and here are some suggestions on how to fix it, and the link to the FAQ, and so on).
The response code itself is general purpose metadata - something primarily intended for general purpose use. By choosing to send an error (4xx or 5xx), you've done most of the important work.
Reasonable candidates here include
409 and 422 share the advantage that they call everyone's attention to the request message-body as the source of the problem. I would guess to use 422 in your case, but it somebody wanted to argue for 409 instead I would certainly listen.
Second question, is it clean to return the partial product in my postByCode with a 307 HTTP Status ?
I don't like 3xx for that case at all. I'd strongly prefer 4xx ("I didn't do it, here are links to the things that we need to do first") or 2xx ("I did some of it, but there are additional steps to consider").
Upvotes: 1