REST API – Processing Partial POST Data in REST

api-designrest

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

Best Answer

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]