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

how to get the request body and correct header of FormData in axios adapter #6232

Open
fancy45daddy opened this issue Feb 13, 2024 · 0 comments

Comments

@fancy45daddy
Copy link

Describe the issue

import axios from 'axios'

async function my(config)
{
    console.log(config)
}

const axios = axios.create({validateStatus:false, adapter:my})
console.log(await axios.postForm('https://httpbin.org/post', {1:2,buf:new Buffer.from('aaa')}).then(_ => _.data))

the output is:

FormData {
  _overheadLength: 200,
  _valueLength: 2,
  _valuesToMeasure: [],
  writable: false,
  readable: true,
  dataSize: 0,
  maxDataSize: 2097152,
  pauseStreams: true,
  _released: false,
  _streams: [
    '----------------------------441912445856199199041598\r\n' +
      'Content-Disposition: form-data; name="1"\r\n' +
      '\r\n',
    '2',
    [Function: bound ],
    '----------------------------441912445856199199041598\r\n' +
      'Content-Disposition: form-data; name="buf"\r\n' +
      'Content-Type: application/octet-stream\r\n' +
      '\r\n',
      <Buffer 61 61 61>,
    [Function: bound ]
  ],
  _currentStream: null,
  _insideLoop: false,
  _pendingNext: false,
  _boundary: '--------------------------441912445856199199041598'
}

and

headers: Object [AxiosHeaders] {
    Accept: 'application/json, text/plain, */*',
    'Content-Type': 'multipart/form-data'
  },

I want to know how to construct the correct request body

----------------------------441912445856199199041598
Content-Disposition: form-data; name="1"

2
----------------------------441912445856199199041598
Content-Disposition: form-data; name="buf"
Content-Type: application/octet-stream

aaa
----------------------------441912445856199199041598--

and the correct content-type

'Content-Type': 'multipart/form-data; boundary=----------------------------441912445856199199041598'

I see there similar code in https://github.com/axios/axios/blob/main/lib/adapters/http.js. But I have no idea how to reuse the source code in https://github.com/axios/axios/blob/main/lib/adapters/http.js.

Example Code

No response

Expected behavior

No response

Axios Version

No response

Adapter Version

No response

Browser

No response

Browser Version

No response

Node.js Version

No response

OS

No response

Additional Library Versions

No response

Additional context/Screenshots

No response

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

No branches or pull requests

1 participant