Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST API response in partial success

Tags:

I have an API which does some bulk processing task. Let's say it does naming of some resource.

I passed 7 request in bulk, out of which 5 updated successfully and 2 failed.

My question is how to handle the response. With HTTP I can't return both success and error at same time.

There is a HTTP code of partial success but I need to return individual response of all resource at once. Is there anyway we can do it?

like image 588
bandi shankar Avatar asked Aug 01 '17 16:08

bandi shankar


People also ask

What is a 206 response?

The HTTP 206 Partial Content success status response code indicates that the request has succeeded and the body contains the requested ranges of data, as described in the Range header of the request.

What is a 201 response code?

The HTTP 201 Created success status response code indicates that the request has succeeded and has led to the creation of a resource.

What is a 202 response?

The 202 response is intentionally non-committal. Its purpose is to allow a server to accept a request for some other process (perhaps a batch-oriented process that is only run once per day) without requiring that the user agent's connection to the server persist until the process is completed.

What is a 207 error code?

207: Multi-Status The default message/response is a text/XML message. It indicates that multiple operations have taken place and that the status for each operation can be viewed in the body of the response.


1 Answers

You may use 207 MULTI-STATUS for http status A Multi-Status response conveys information about multiple resources in situations where multiple status codes might be appropriate.

The default Multi-Status response body is a HTTP entity with a 'multistatus' root element. Further elements contain 200, 300, 400, and 500 series status codes generated during the method invocation.

You can have an array of response objects within the body of the response and those objects may have it's own status codes

Example

HTTP 207

{     "data": [         {             "messgage": "success",             "resource": {                 "foo": "bar",                 "id": "1d1"             },             "status": 200         },         {             "messgage": "Requested resource or subresource not found ",             "resource": null,             "status": 404         },         {             "messgage": "success",             "resource": {                 "foo": "bars",                 "id": "1d2"             },             "status": 200         }     ],     "metadata": {         "failure": 1,         "success": 2,         "total": 3     } } 
like image 83
Sarath Sadasivan Pillai Avatar answered Sep 18 '22 05:09

Sarath Sadasivan Pillai