I have a resource that accepts array of numbers. Each number is proceed interdependently, and computation fails are expected. How should I communicate to API client "some of your numbers couldn't be proceed, but rest was successfully forwarded"?
-
I think it depends on how you access the resource and the structure of the resource itself. Can you include a bit more detail? Such as which verb you are using, the structure of the resource, and the nature of the partial failure?MetaFight– MetaFight2015年02月05日 14:31:02 +00:00Commented Feb 5, 2015 at 14:31
-
1You shouldn't. REST is stateless, each request is independent of the last and the next. If you are updating a resource to a new state that update should not depend on a previous request. If that update fails because the server will not allow the resource to be put into the state the client has put it in then return a 403 explaining why the server has forbidden that new state.Cormac Mulhall– Cormac Mulhall2015年02月06日 18:17:33 +00:00Commented Feb 6, 2015 at 18:17
-
@CormacMulhall This really should be an answer. It is much better than the currently accepted answer as that one condones breaking principles of RESTmaple_shaft– maple_shaft ♦2015年02月10日 13:08:31 +00:00Commented Feb 10, 2015 at 13:08
1 Answer 1
It seems the most straightforward method is just returning a XML or JSON, with the numbers and status.
Per your description, I imagine a structure like this would suffice:
{
requestDate: '2015-02-05 12:32'
results: [{
number: 1,
status: 'forwarded',
},
{
number: 2,
status: 'forwarded',
},
{
number: 3,
status: 'fail',
}]
}
I also think a HTTP Status of 200 is most indicated, as the client request was successfully processed. Also you can read in the spec (emphasis mine):
10.2.1 200 OK
...
POST an entity describing or containing the result of the action;
Edit: Researching a little more about the status codes, I think it may be better to use 207 Multi-Status
:
The message body that follows is an XML message and can contain a number of separate response codes, depending on how many sub-requests were made.[4]
-
Yes, it was my first idea. But should I use 200 HTTP status code if some of "subrequests" have failed?Ginden– Ginden2015年02月05日 14:34:51 +00:00Commented Feb 5, 2015 at 14:34
-
-
3This smells of a fudged REST implementation.MetaFight– MetaFight2015年02月05日 15:02:39 +00:00Commented Feb 5, 2015 at 15:02
-
1I disagree about the 4xx, it is for client errors, per the description of the OP, is appear to be a problems with the server proccess. Maybe it would be more indicated to use a "207 Multi-Status"RMalke– RMalke2015年02月06日 19:56:18 +00:00Commented Feb 6, 2015 at 19:56
-
1Depends on what caused the error. If the client is trying to put resource into a state the server won't allow due to an expected rule on the servers side then that is a 4xx since the client is making mistake. But yes if the server is having unexpected error that is a 5xx. Planning to have an unexpected error though is bad design.Cormac Mulhall– Cormac Mulhall2015年02月10日 13:19:06 +00:00Commented Feb 10, 2015 at 13:19