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

Posting formdata may result in request header 'Content-Length': 'NaN' being set #5534

Closed
thhermansen opened this issue Feb 6, 2023 · 2 comments · Fixed by #5535
Closed

Comments

@thhermansen
Copy link

thhermansen commented Feb 6, 2023

Describe the bug

Passing a FormData object to Axios client may result in request header 'Content-Length': 'NaN' being set.

To Reproduce

Given the following code:

// client is an Axios instance
const pdf = await client.get(pdfUrl, { responseType: 'stream' });

const form = new FormData();
form.append('file', pdf.data, { filename, contentType: 'application/pdf' });

const res = await client.post(urlToUploadService, form);

We get the following request header added:

    headers: AxiosHeaders {
      Accept: 'application/json, text/plain, */*',
      'Content-Type': 'multipart/form-data; boundary=--------------------------852062351748726458865935',
      'User-Agent': 'axios/1.3.2',
      'Content-Length': 'NaN',
      'Accept-Encoding': 'gzip, compress, deflate, br'
    },

I believe 'Content-Length': 'NaN', is not something we want :) I'm pretty sure the header was not added with Axios version 1.2.6.


I believe this is related code:

axios/lib/adapters/http.js

Lines 266 to 267 in 0b44929

const knownLength = await util.promisify(data.getLength).call(data);
headers.setContentLength(knownLength);

If we log form's getLength:

  form.getLength((err, length) => {
    console.log(err, length);
  }),

We'll get a hint of what is going on:

null NaN

Axios Version

1.3.2

Adapter Version

No response

Browser

No response

Browser Version

No response

Node.js Version

v18.14.0

OS

macos 13.2

Additional Library Versions

No response

Additional context/Screenshots

@DigitalBrainJS
Copy link
Collaborator

Thanks for the report! I can't reproduce the error, but we'll add an isFinite check on our side, just in case. Apparently, there are some unhandled cases under which the method returns NaN instead of calling its callback with an error.

@thhermansen
Copy link
Author

Thank you! :)

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