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

Error while posting buffer form data #1892

Closed
filips123 opened this issue Nov 25, 2018 · 4 comments
Closed

Error while posting buffer form data #1892

filips123 opened this issue Nov 25, 2018 · 4 comments

Comments

@filips123
Copy link

filips123 commented Nov 25, 2018

Description:

I want to post form data with buffer to URL, but I get error The "string" argument must be one of type string, Buffer, or ArrayBuffer.

Example:

const axios = require('axios')
const FormData = require('form-data')

let avatar = Buffer.from(['00', '01', '03', '04', '05', '06', '07', '08', '09']) // Buffer data of image file
let url = 'https://example.com/' // Some URL

let formData = new FormData()
formData.append('avatar', avatar)

await axios.post(url, formData, { // Await is called inside async function
    headers: {
        'Content-Type': `multipart/form-data; boundary=${formData._boundary}`
    }
})

Error:

TypeError [ERR_INVALID_ARG_TYPE]: The "string" argument must be one of type string, Buffer, or ArrayBuffer. Received type object
      at Function.byteLength (buffer.js:516:11)
      at exports.XMLHttpRequest.send (node_modules/xmlhttprequest/lib/XMLHttpRequest.js:363:80)
      at dispatchXhrRequest (node_modules/axios/lib/adapters/xhr.js:178:13)
      at new Promise (<anonymous>)
      at xhrAdapter (node_modules/axios/lib/adapters/xhr.js:12:10)
      at dispatchRequest (node_modules/axios/lib/core/dispatchRequest.js:59:10)
      at process._tickCallback (internal/process/next_tick.js:68:7)

Environment:

  • NodeJS: 10.11.0
  • FormData: 2.3.3
  • Axios: 0.18.0
@filips123
Copy link
Author

This is similar to #318. However, it is old and closed and I could not fix it.

I've tried #318 (comment), but I still have same error.

@filips123
Copy link
Author

I've fixed this using formData._streams instead of just formData. I've also added formData.getHeaders() to headers and some other changes (see form-data/form-data#414).

This is probably some temporary and quick solution so this issue should not be closed until it works better.

const axios = require('axios')
const FormData = require('form-data')

let avatar = Buffer.from(['00', '01', '03', '04', '05', '06', '07', '08', '09']) // Buffer data of image file
let url = 'https://example.com/' // Some URL

let formData = new FormData()
formData.append('avatar', avatar)

let data = ''
for (var i = 0, len = formData._streams.length; i < len; i++) {
  if (typeof formData._streams[i] !== 'function') {
    data += formData._streams[i] + '\r\n'
  }
}

await axios.post(url, data, { // Await is called inside async function
    headers: formData.getHeaders()
})

@Rmsilva1
Copy link

Rmsilva1 commented Feb 19, 2020

I've fixed this using formData._streams instead of just formData. I've also added formData.getHeaders() to headers and some other changes (see form-data/form-data#414).

This is probably some temporary and quick solution so this issue should not be closed until it works better.

const axios = require('axios')
const FormData = require('form-data')

let avatar = Buffer.from(['00', '01', '03', '04', '05', '06', '07', '08', '09']) // Buffer data of image file
let url = 'https://example.com/' // Some URL

let formData = new FormData()
formData.append('avatar', avatar)

let data = ''
for (var i = 0, len = formData._streams.length; i < len; i++) {
  if (typeof formData._streams[i] !== 'function') {
    data += formData._streams[i] + '\r\n'
  }
}

await axios.post(url, data, { // Await is called inside async function
    headers: formData.getHeaders()
})

Im having the same issue, but i have to send 3 query parameters along with the request. i did not managed to make it work with your solution. :(

@chinesedfan
Copy link
Collaborator

In fact, it is an issue of package form-data. And according to #2049, it has been solved.

@axios axios locked and limited conversation to collaborators May 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants