Incorrect calculation of content-length for string or URLSearchParams bodies
glasser opened this issue · 0 comments
glasser commented
got-fetch
calculates the HTTP content-length header like this for string bodies:
'Content-Length': String(body.length)
However, body.length is defined as UTF-16 codepoints (roughly, "characters") whereas content-length
is defined as bytes. So for any non-ASCII body, this will set a content-length
that is too small.
I am a little surprised that this isn't something that you can just rely on got
to handle for you rather than calculating it yourself. got
has a whole file dedicated to calculating this correctly: https://github.com/sindresorhus/got/blob/835c70bd6fdc5b1ba7e7ffd322490b6d66fb407e/source/core/utils/get-body-size.ts