Skip to content

Content-Length header is not set even if form-data uses knownLength property  #5275

Closed

Description

@BaCaRoZzo

Describe the bug

I need to send a big file of ~80MB to a lighthttp server which accepts it as a form. For the purpose I use form-data since it is supported by axios. According to the documentation and several sources online the following should work:

const formdata = require('form-data')
const fs = require('fs')
const https = require('https')
const axios = require('axios')

function _postRequest(file) {
    const request = {
        url: `https://<host>:8080/<endpoint>`,
        method: 'POST',
        body: new formdata(),
        maxContentLength: Infinity,
        maxBodyLength: Infinity,
    }
    request.data.append('file', fs.createReadStream(file), {
        knownLength: fs.statSync(file).size,
    })
    request.headers = request.data.getHeaders()
    //NOTE workaround for self-signed certificates
    request.httpsAgent = new https.Agent({ rejectUnauthorized: false })
    return axios(request)
}

However the request is rejected by my server. Logging with NODE_DEBUG=http (sorry I had also an interceptor log but I deleted it apparently) results in the following snippet:

{
  maxRedirects: 21,
  maxBodyLength: Infinity,
  protocol: 'https:',
  path: null,
  method: 'POST',
  headers: {
    Accept: 'application/json, text/plain, */*',
    'Content-Type': 'multipart/form-data; boundary=--------------------------375821630796827194957398',
    'User-Agent': 'axios/0.27.2',
    'content-type': 'multipart/form-data; boundary=--------------------------375821630796827194957398'
  },

On the other hand, the corresponding node-fetch function seems to work fine. Here is the code:

const formdata = require('form-data')
const fs = require('fs')
const https = require('https')
const fetch = require('node-fetch')

function _postRequest( file) {
    const options = {
        method: 'POST',
        body: new formdata()
    }
    options.body.append('file', fs.createReadStream(file), {
        knownLength: fs.statSync(file).size,
    })
    options.headers = options.body.getHeaders()
    //NOTE workaround for self-signed certificates
    //if (true) {
    options.agent = new https.Agent({ rejectUnauthorized: false })
    //}
    return fetch(`https://<host>:8080/<endpoint>`, options)
}

And the logs look better:

headers: [Object: null prototype] {
    'content-type': [
      'multipart/form-data; boundary=--------------------------948324181019601509098137'
    ],
    Accept: [ '*/*' ],
    'Content-Length': [ '81992924' ],
    'User-Agent': [ 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)' ],
    'Accept-Encoding': [ 'gzip,deflate' ]
  },

To Reproduce

No response

Code snippet

No response

Expected behavior

I would expect axios to correctly calculate the Content-Length and add it to the set of headers, much like node-fetch seems to do.

Axios Version

0.27.2, 1.1.3

Adapter Version

No response

Browser

No response

Browser Version

No response

Node.js Version

14.20.0

OS

Fedora Linux 36

Additional Library Versions

Additional context/Screenshots

No response

Activity

  1. cyberwombat commented on Nov 24, 2022

    @cyberwombat

    I believe you still need to set the content-length header. Here's how I do it:

    const formData = new FormData()
    const stream = createReadStream(path)
    const { size } = statSync(path)
    formData.append('file', stream, { knownLength: size })
    const headers = {
        ...formData.getHeaders(),
        'Content-Length': formData.getLengthSync()
    }
    ...
    
  2. BaCaRoZzo commented on Nov 25, 2022

    @BaCaRoZzo
    Author

    @cyberwombat thanks for chiming in. I think I've tried several combinations but maybe not this. 🤔
    I'll give it a go asa I can get around the code that was failing and report back if it works.


    EDIT

    Yes, I can confirm it works correctly by explicitly setting the length as proposed in the previous message. Thanks again to @cyberwombat for the resolutive hint!

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