Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FormData with invalid Content-Length #1787

Open
gbrueckl opened this issue Nov 10, 2023 · 2 comments
Open

FormData with invalid Content-Length #1787

gbrueckl opened this issue Nov 10, 2023 · 2 comments
Labels

Comments

@gbrueckl
Copy link

I want to upload a file using FormData but it fails with

{"code":"InputStreamIncorrectLengthException","message":"The length of the file input stream does not match the provided length. Expected: '12463', Actual (approximate): '12464.'"}

As I am not setting the Content-Length manually but its set internally by node-fetch, I want to raise this here.
PS: I also tried setting the Content-Length manually to 12463, 12464 and 12465 (the actual length of the UInt8Array) but none worked either

Reproduction

I am using the following code from within a VSCode extension

let formData: FormData = new FormData();
const fileName = uri.path.split('/').pop().split(".")[0];

const arr: Uint8Array = await vscode.workspace.fs.readFile(uri);
const blob: Blob = new Blob([arr]);
formData.append(fileName, blob);

let headers = this._headers;
headers["Content-Type"] = "multipart/form-data";

const config: RequestInit = {
method: "POST",
headers: headers,
body: formData
};
let response: Response = await fetch(endpoint, config);

Expected behavior

Content-Length should be set correctly automatically when using FormData
the code is similar to whats described in the docs: const abc = new File([binary], 'abc.txt', { type: 'text/plain' })

Your Environment

software version
node-fetch 3.3.2
node v18.13.0
npm 8.19.3
Operating System Windows 11
@gbrueckl gbrueckl added the bug label Nov 10, 2023
@kirbdee
Copy link

kirbdee commented Nov 16, 2023

unclear if it's a similar issue. but the services i was using is also expectin a multipart/form-data body but was responding with bad request. I then just omitted the content-type for sanity and it seemed to work, this was how the samples used it

https://www.npmjs.com/package/node-fetch#post-data-using-a-file

import fetch, { FormData, File, fileFrom } from 'node-fetch'

const httpbin = 'https://httpbin.org/post'
const formData = new FormData()
const binary = new Uint8Array([ 97, 98, 99 ])
const abc = new File([binary], 'abc.txt', { type: 'text/plain' })

formData.set('greeting', 'Hello, world!')
formData.set('file-upload', abc, 'new name.txt')

const response = await fetch(httpbin, { method: 'POST', body: formData })
const data = await response.json()

console.log(data)

@GeoffreyPlitt
Copy link

+1 I am seeing this too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants
@GeoffreyPlitt @kirbdee @gbrueckl and others