Skip to content

hackney: provide content-length header for empty requests - #239

Open
aeruder wants to merge 1 commit into
aws-beam:masterfrom
aeruder:master
Open

hackney: provide content-length header for empty requests#239
aeruder wants to merge 1 commit into
aws-beam:masterfrom
aeruder:master

Conversation

@aeruder

@aeruder aeruder commented Jul 24, 2026

Copy link
Copy Markdown

This works around a subtle change in hackney between 1.xx and 4.x.x - newer hackney does not provide the (optional by spec!) "content-length: 0" field for 0-length requests. While clients are free to not provide the content-length in these situations, amazon does require "content-length: 0" for 0-byte POSTs. Without this change some requests will fail with a 411 (like writing a 0-byte file to s3).

This change adds the "content-length: 0" header for 1.xx and 4.x.x - both are fine if the header was already present.

We could calculate and add for all requests but as hackney 4.x.x is written now that ends up being a bit of a waste of cycles as hackney always calculates the content-length, it just may not add the result to the headers if the header is already present.

This works around a subtle change in hackney between 1.xx and 4.x.x -
newer hackney does not provide the (optional by spec!)
"content-length: 0" field for 0-length requests.  While clients are
free to not provide the content-length in these situations, amazon
does require "content-length: 0" for 0-byte POSTs.  Without this
change some requests will fail with a 411 (like writing a 0-byte
file to s3).

This change adds the "content-length: 0" header for 1.xx and 4.x.x -
both are fine if the header was already present.

We could calculate and add for all requests but as hackney 4.x.x is
written now that ends up being a bit of a waste of cycles as hackney
always calculates the content-length, it just may not add the result
to the headers if the header is already present.
@aeruder aeruder changed the title hackney: always provide content-length header hackney: provide content-length header for empty requests Jul 24, 2026
@aeruder

aeruder commented Jul 24, 2026

Copy link
Copy Markdown
Author

Fix for #238

@aeruder

aeruder commented Jul 24, 2026

Copy link
Copy Markdown
Author

I have a small utility locally that "creates a directory" on s3 (which is really just a 0-byte put_object) and prior to this change get:

an error occurred: {:error, {:unexpected_response, %{body: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>MissingContentLength</Code><Message>You must provide the Content-Length HTTP header.</Message><RequestId>...

afterwards it is fine.

@aeruder

aeruder commented Jul 24, 2026

Copy link
Copy Markdown
Author

I'll also note I've checked this change against hackney 1.25 and 4.6.0 and both are fine with it in place (although obviously 1.25 was fine prior to this change as well).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant