Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

413 Payload Too Large on Django server

My team has been getting 413 errors whenever we try and upload large files to our Django back-end: 413 Payload too large

We can't exactly pin down the maximum acceptable file size - it seems to vacillate in the 1-3MB range.

Things we have excluded:

  • It's not a webserver configuration issue, because we're running the
    Django server locally (without a webserver)

  • We believe it's not an app server configuration issue, because this happens on multiple app servers (./manage.py runserver and daphne -p 8000 topknott.asgi:application)

  • It's not an issue with the field on the Django model, which looks normal: photo = models.ImageField(blank=True)

Can anyone spot what we're missing?

like image 595
NattyP Avatar asked Mar 15 '19 20:03

NattyP


People also ask

How does Django handle 413 error?

your HTTPS server does not have client_max_body_size configured, so it is defaulting to 1M & causing this 413 (Request Entity Too Large) error. To fix this issue, you simply need to add client_max_body_size to BOTH the HTTP server block and the HTTPS server block, as shown in the example below: http { ...

What is 413 request entity Too Large?

What does “413 Request Entity Too Large” mean? A 413 HTTP error code occurs when the size of a client's request exceeds the server's file size limit. This typically happens when a client attempts to upload a large file to a web server, and the server responds with a 413 error to alert the client.


2 Answers

Django has a build in mechanism to prevent any suspicious activity.

In your settings.py file set the variable

DATA_UPLOAD_MAX_MEMORY_SIZE = 10*1024*1024  # your size limit in bytes

See documentation: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-DATA_UPLOAD_MAX_MEMORY_SIZE

like image 76
Martin Janeček Avatar answered Sep 26 '22 06:09

Martin Janeček


If your team was/is using Django Channels, there was a piece of code introduced in 2.1.7 causing an unintended 413 error (discussed here). This was fixed in 2.3.0, however.

like image 28
Dabble Avatar answered Sep 23 '22 06:09

Dabble