27

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?

2
  • 2
    Practically infinite ways. This is a design decision you'll need to make on your own, based on your particular situation. Commented Aug 1, 2017 at 16:19
  • This isn't a Go question at all, it's an HTTP/API question. You can handle this in countless ways, and the best one depends on your needs and possibly tastes. Commented Aug 1, 2017 at 20:02

1 Answer 1

28

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 their own status codes

Example

HTTP 207

{
    "data": [
        {
            "message": "success",
            "resource": {
                "foo": "bar",
                "id": "1d1"
            },
            "status": 200
        },
        {
            "message": "Requested resource or subresource not found",
            "resource": null,
            "status": 404
        },
        {
            "message": "success",
            "resource": {
                "foo": "bars",
                "id": "1d2"
            },
            "status": 200
        }
    ],
    "metadata": {
        "failure": 1,
        "success": 2,
        "total": 3
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

@bandishankar please check this example, it does not cover all possibilities but serves as a basic sample
how valid is this sample response? the relevant RFC seems to indicate that the response is meant to be in XML format and follow a particular structure, which the sample response doesn't do.
The same spec suggests the default Multi-Status response body is a text/XML or application/xml. So json is valid as far as the content-type is json
I have a situation where i need to add object created/updated as well as error object in the response. e.g. 2 entities created but 3 had issues. Does anyone have a example code which can help?
I've seen other answers to this same question that prefer to give a 200 OK response for scenarios like this - however this answer seems to be the better answer. It's better designed, more logical, and informative. This way API client's don't have to do any logic for 200 OK responses. Also most API management tools will track metrics per API response code and it's better informational purposes to track these separately. Example: you upgrade your API's database and now see a spike in "207" response codes in your API management tool. Now you know to investigate.
|

Your Answer

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.