Home > database >  Appropriate HTTP status code for file upload
Appropriate HTTP status code for file upload

Time:11-18

My application allows file upload. As a restriction I want to restrict files with only certain extension (say pdf for example)

So in case a file with pdf extension is uploaded, I return 200/201 ok status code to indicate the successful upload.

I am confused, what should I return for non-pdf files after reading multiple answers-

  1. 400 - argument to return 400 bad request is that request failed due to validation. Argument to not to use is sometime it also indicates syntactical error while making the request.
  2. 415 - argument to return 415 Unsupported Media is that file extension is not same. (Considering Media as file), but content type (multipart/form-data) matches and is supported.
  3. 422 - Argument to use 422 unprocessable entity is that server understands the content-type (multipart/form-data) but still can't process the request. But other though is, it can still process if the use case is only to upload the file.

CodePudding user response:

From MDN:

415 Unsupported Media Type

The HTTP 415 Unsupported Media Type client error response code indicates that the server refuses to accept the request because the payload format is in an unsupported format.

The format problem might be due to the request's indicated Content-Type or Content-Encoding, or as a result of inspecting the data directly.

I think the last sentence specifically suggests that 415 is not strictly for unsupported Content-Type headers.

  • Related