Skip to content

Error: Cannot calculate proper length in synchronous way. #5520

Closed

Description

@LockBlock-dev

Describe the bug

Relying on axios automatic serialization to create a form-data results in this error. The following lines are responsible of this error : https://github.com/axios/axios/blob/release/dist/node/axios.cjs#L2654 (mainly the getLengthSync function)

According to form-data themselves, using getLengthSync is not recommended.

To Reproduce

  • Create a file buffer with fs.createReadStream
  • Set HTTP header Content-Type to multipart/form-data to make use of automatic serialization
  • Get the form-data Cannot calculate proper length in synchronous way. error

Code snippet

const axios = require("axios");
const fs = require("node:fs");
const fileBuffer = fs.createReadStream("./file.ext");

axios.post('https://httpbin.org/post', {file: fileBuffer}, {
  headers: {
    'Content-Type': 'multipart/form-data'
  }
})
.then(({data}) => console.log(data))
.catch((err) => console.error(err));

Expected behavior

The content length should be calculated without throwing any error.

Axios Version

1.3.0

Adapter Version

No response

Browser

No response

Browser Version

No response

Node.js Version

16.19.0

OS

No response

Additional Library Versions

No response

Additional context/Screenshots

No response

Activity

  1. DigitalBrainJS commented on Feb 1, 2023

    @DigitalBrainJS
    Collaborator

    I wanted to avoid this so as not to introduce an anti-pattern due to one asynchronous call inside the adapter root promise, and defer this until the module is refactored, but since the community wants it... :)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions