简体   繁体   中英

http code for partual success / error

I have a service that takes a list of items to handle.

Each of the items is handeled one at the time in the backend, when all items is done the service returns a document containing the original list of items but with a success or failure status on each line.

example:

PUT - body:

[
  {"item" : 1},
  {"item" : 2},
  {"item" : 3}
]

reponse - body:

[
  {"item" : 1, "state" : "OK"},
  {"item" : 2, "state" : "OK"},
  {"item" : 3, "state" : "FAILED"}
]

The question is now: which return code should I use if one of the items has failed? I cannot seem to find any correct matching http status code for this, it is a failure, but then again its not, but allmost;) ?

From the HTTP point of view, it's a success - I'd return 200 - the request was successfully received and processed.

It's then up to the app to check if it's all OK.

However, to make life easier I might add an error field into the response?

A better idea would be to use a 207 multi-status response, but to send the data in a POST.

This happens a lot with things that upload/update a lot of items. In the multistatus response, you can correspond each action and a specific response, like so:

<response>
  <item id="1">
    <status>200 OK</status>
  </item>
  <item id="2">
    <status>403 Forbidden</status>
  </item>
</response>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM