0

I'm currently consuming a REST API endpoint for bulk creation of entities via csv file upload.

POST /entity/csv
Content-Type: multipart/form-data

With form-data:

key value
file binary

The backend is currently returning a 400 status code when it can't parse the csv file, however the request sent from the client is actually correct.

From my point of view, this is not the way to go, and it should return a 200, with additional info on the parsing issues. Currently, the parsing errors are on the 400 response body.

Is there any case to be made for the 400 status code when there are failures in the parsing? E.g. "required field is empty - line 43"

What's the consensus around this?

I'm asking this as a frontend developer, because I'm writing frontend logic for displaying the parsing errors to the user inside a catch block, which seems odd to me.

1 Answer 1

0

What's the consensus around this?

Status codes are metadata of the "transfer documents over a network domain".

If you are dealing with a request like...

POST /entity/csv HTTP/?.?
Content-Type: text/csv

a,b,c....

Then the guidelines you want to follow are probably those in RFC 4918

  • If the body isn't actually a CSV document, then use a 400 Bad Request
  • If the body is the wrong CSV document, then use 422 Unprocessable Entity.

In practice, while there is a semantic difference between 400, 422, 403, etc..., it's not clear that general purpose clients can do anything with those differences.

With either code, the body of the response would be "a representation containing an explanation of the error". It's in this place that you would put your specific notes about what problems were encountered by the parser.


The reason that it wants to be a 4xx rather than a 2xx is caching, and cache-invalidation.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi there, I have to clarify: the content-type is multipart/form-data. With the request body being { file: <binary> }. I edited the original question

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.