4

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"?

maple_shaft
26.6k12 gold badges60 silver badges136 bronze badges
asked Feb 5, 2015 at 14:19
3
  • 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? Commented Feb 5, 2015 at 14:31
  • 1
    You 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. Commented 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 REST Commented Feb 10, 2015 at 13:08

1 Answer 1

2

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]

answered Feb 5, 2015 at 14:33
7
  • Yes, it was my first idea. But should I use 200 HTTP status code if some of "subrequests" have failed? Commented Feb 5, 2015 at 14:34
  • I just edited the answer Commented Feb 5, 2015 at 14:40
  • 3
    This smells of a fudged REST implementation. Commented Feb 5, 2015 at 15:02
  • 1
    I 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" Commented Feb 6, 2015 at 19:56
  • 1
    Depends 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. Commented Feb 10, 2015 at 13:19

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.