'HTTP4s increase max upload size

I am toying with http4s multipart file upload, which I got working. However, the multipart parsing throws an exception for file uploads bigger than ~500kb.

The error on client side, which is thrown while parsing multipart body is HTTP 422: The request body was invalid.

The error on the server side is "Part not terminated properly"

Since this is obviously related to the size of uploaded file, I suspect there must be a config in http4s to allow larger uploads?

Thanks in advance!



Solution 1:[1]

can you try changing content length in headers?

eg: content-length: 3495 OR Content-Length: 3495 depending on size of your content.

Ref: https://github.com/http4s/http4s/blob/4b928e0dc0ba6edbdbe7461204663e13a7013f8c/blaze-server/src/main/scala/org/http4s/blaze/server/Http2NodeStage.scala#L129

As I see this method getBody is called with len param https://github.com/http4s/http4s/blob/4b928e0dc0ba6edbdbe7461204663e13a7013f8c/blaze-server/src/main/scala/org/http4s/blaze/server/Http2NodeStage.scala#L108

What I uncovered is that you can pass this header content-length to allow that size

https://github.com/http4s/blaze/blob/3d1b15eace96740507daac9c9e75f978bbd2e524/http/src/main/scala/org/http4s/blaze/http/HeaderNames.scala#L26

ref for content length header: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Length

Hope it works.

Solution 2:[2]

There is a header to change the maximum size able to be uploaded called Content-Length.

This is the simple syntax:

Content-Length: <length>

The length parameter is just a number stating the number of bytes that is allowed for a maximum size.

Some examples include:

Content-Length: 6553
Content-Length: 54138

So, you can set the maximum size here.


To inspect this header in browsers, follow the steps below.

  1. Click Inspect Element in your browser.
  2. Click on the Network tab.
  3. Check the request header.
  4. Find the header Content-Length in there.

If you want browser compatibility statistics, here they are:

  • Google Chrome (and all Chromnium-based browsers)
  • Firefox
  • Opera
  • Safari
  • Microsoft Internet Explorer

This is how you can change the maximum file upload size with HTTP4.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 swapyonubuntu
Solution 2