Skip to content

Conversation

@erunion
Copy link
Contributor

@erunion erunion commented Sep 15, 2021

Since #227 has us a bit spooked we've started testing other targets that may also have issues with multipart/form-data requests and found that both of the fetch targets for Node and JS are not currently transferring data properly because the custom header that is being added is clashing with the auto-generated header from the FormData API and form-data module.

You can see this problem in action with the multipart-form-data snippet against https://httpbin.org/anything:

const FormData = require('form-data');
const fetch = require('node-fetch');
const formData = new FormData();

formData.append('foo', 'bar');

let url = 'https://httpbin.org/anything';

const options = {
  method: 'POST',
  headers: {'Content-Type': 'multipart/form-data; boundary=---011000010111000001101001'}
};

options.body = formData;

fetch(url, options)
  .then(res => res.json())
  .then(json => console.log(json))
  .catch(err => console.error('error:' + err));
{
  args: {},
  data: '',
  files: {},
  form: {},
  headers: {
    Accept: '*/*',
    'Accept-Encoding': 'gzip,deflate',
    'Content-Length': '161',
    'Content-Type': 'multipart/form-data; boundary=---011000010111000001101001',
    Host: 'httpbin.org',
    'User-Agent': 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)',
    'X-Amzn-Trace-Id': 'Root=1-61428408-5a6abe107e16c34466e8e6ba'
  },
  json: null,
  method: 'POST',
  origin: 'REDACTED',
  url: 'https://httpbin.org/anything'
}

After removing the multipart/form-data header:

{
  args: {},
  data: '',
  files: {},
  form: { foo: 'bar' },
  headers: {
    Accept: '*/*',
    'Accept-Encoding': 'gzip,deflate',
    'Content-Length': '161',
    'Content-Type': 'multipart/form-data;boundary=--------------------------635295776437093815404580',
    Host: 'httpbin.org',
    'User-Agent': 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)',
    'X-Amzn-Trace-Id': 'Root=1-6142841e-4c789faa2a1d423452674f13'
  },
  json: null,
  method: 'POST',
  origin: '98.42.248.146',
  url: 'https://httpbin.org/anything'
}

const stringifyObject = require('stringify-object')
const code = new CodeBuilder(opts.indent)

options = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed this variable to reqOpts so it's not overwriting the options argument of the target.

Copy link
Contributor

@reynolek reynolek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @erunion -- I figured there might be a few other places multipart was broken.

@erunion erunion closed this Apr 29, 2022
@erunion erunion deleted the fix/multipart-header-issues branch April 29, 2022 22:53
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

Successfully merging this pull request may close these issues.

3 participants