3

I'm implementing a REST method that allows you to post a list of items. If all data is correct I obviously return a 201 and if all data is incorrect a 400.

What would be an appropriate response code if some items are correct and others are not?

2 Answers 2

4

Send a 400. Because the request is bad, even if part of it is good. It is bad because you can't act on what you've received.

4
  • There's something to say for that, but on the other hand, I can act on the part that was correct. In this case, it's about a set of images, so I don't want the user to reupload the correct images again. I guess indicating the correct ones in the response would be sufficient for that. However, being nitpicky, the spec says: "The server cannot or will not process the request, due to a client error ", which is not entirely correct, because part of it will be processed
    – Kenneth
    Oct 21, 2013 at 22:03
  • 3
    @Kenneth If you want to prevent having to re-send a lot of data, why not just break upload down into a one-image-per-request pattern. That way you can have the status be updated on the client for every image that's successfully loaded and give the user the option to re-send the ones that didn't make it. Oct 21, 2013 at 22:35
  • I also want to access the API through an HTML form and allow them to select multiple files at once. I suppose I could intercept the form submit with JS and send various posts. Good idea! Thanks!
    – Kenneth
    Oct 21, 2013 at 22:47
  • 1
    Please note that 400 "Bad request" is not for invalid data. It is meant to be used when the request could not be understood. Invalid data can be understood it is just not valid to process further. ("The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications." as per restpatterns.org/HTTP_Status_Codes/400_-_Bad_Request) Oct 22, 2013 at 8:11
3

If you are willing to step slightly outside pure HTTP responses, you could go the same way as the WebDAV protocol has done. They have extended the HTTP protocol with, among other things, a "multi-status response" (using code 207), that contains a status response for each individual item in its body.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.