0

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!

Yogesh
  • 78
  • 6

2 Answers2

0

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.

Arnav Thorat
  • 3,078
  • 3
  • 8
  • 33
  • This is an image file, so unfortunately I cant make it smaller. The api would also allow video uploads, so limiting uploads to 500kb is not really an option. Can you point me to how to configure the max upload setting? – Yogesh Apr 11 '22 at 14:14
  • I've found it (after lots of research)! There is a header called `Content-Length`. I'll add that in my answer. – Arnav Thorat Apr 11 '22 at 19:30
  • As I pointed out in the next post, content length is being posted as part of the request. That doesn’t prevent the request with large payload being rejected. Sorry – Yogesh Apr 12 '22 at 20:15
  • Oh, okay. I'll try and find another solution. – Arnav Thorat Apr 12 '22 at 20:49
0

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.

swapyonubuntu
  • 1,952
  • 3
  • 25
  • 34