Reputation: 49097
I am writing a RESTful web service API. I am going to post an Answer
resource to a mechanical question. When I do this I want to know if I answered correct or not. For this I have a Result
resource.
Is it wrong to return a different resource that the one you POSTed in a HTTP request?
Example:
POST /mechanical-questions/4/answers
- shoud return result.
The answer will be saved on the server, but so will the result. Should I return a HttpStatus 20x code together with a Location header indicating where I can find the quiz result or the answer, and what should the response entity be? The created answer or the quiz result?
Upvotes: 3
Views: 161
Reputation: 230
Obviously there is no one 'correct' answer to this. I think you are right in saying that you should not return a RESULT object to an endpoint that receives an ANSWER object. To me I think it is a bit strange to return the Location: header. It should be up to the consuming client what they want to do with the ANSWER object after creation. What I sometimes see is a location property in the POST response object.
So you have a request like this
{
'answer' : 'I hope I am correct'
}
And a response like this
{
'id' : 12345
'answer' : 'I hope I am correct'
'result_id' : 68472
'result_location' : '/mechanical-questions/4/result/68472'
}
This allows the client to determine the flow after a user makes an answer - maybe they immediately do a GET for the result and display it, or maybe the bundle up a multi-get response after a series of questions are answered.
Upvotes: 2
Reputation: 5049
I think you should certainly return an OK code since it is a completely valid situation. It is nor a server error or bad request or something like that.
As for the response entity, that really depends on your business logic, at least from my point of view. It's a bit controversial between developers. If you want your client to get a correct answer in either case then just put the answer itself. If the client should maybe have a few tries before he gets a answer, than maybe put a URL to the response entity. You could even do both...
Upvotes: 0