Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP status code to return by a REST API when image size exceeds limit

A POST to a specific end point allows to upload an image except if the image is too large, so I want to return the appropiate http status code response in that case.

A http status code 400 response it does not seem to fit well in this case.

400 Bad Request: "The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

I think that the image being too large it does not imply that the request is malformed or syntactically incorrect.

Any suggestions?

like image 650
rfc1484 Avatar asked Jan 15 '16 11:01

rfc1484


People also ask

What is a 309 status code?

309-399: Unassigned Status codes 309 through 399 are currently unassigned.

What is a 299 status code?

HTTP response codes 200 – 299 are bearers of good news: the request has been accepted, a new request has been created, or a certain problem was solved.

Why does REST API have 204 status code?

The HTTP 204 No Content success status response code indicates that a request has succeeded, but that the client doesn't need to navigate away from its current page.

When should I use HTTP 203?

The HTTP 203 Non-Authoritative Information response status indicates that the request was successful but the enclosed payload has been modified by a transforming proxy from that of the origin server's 200 ( OK ) response .


2 Answers

You can use 420 or even 422, but I would avoid that until you have really good reason to have separate code for it. Usually is better to keep number of different status codes rather small. Check top 10 on that list: http://www.restapitutorial.com/httpstatuscodes.html

You should avoid using more than 10 codes, because your API will become too complex.

So my answer is: use 400 with proper error message returned to the client like: "Image too large, you can upload files up to XX MB"

like image 181
Piotr Bochynski Avatar answered Sep 29 '22 03:09

Piotr Bochynski


This seems like it would be an ideal candidate for 413 Payload Too Large. From Section 6.5.11 of RFC 7231:

The 413 (Payload Too Large) status code indicates that the server is refusing to process a request because the request payload is larger than the server is willing or able to process.

like image 44
Joel Aelwyn Avatar answered Sep 29 '22 03:09

Joel Aelwyn