Skip to content

Content of form data does not persist leading to socket hang up #1513

Answered by Giotino
sunknudsen asked this question in Q&A
Discussion options

You must be logged in to vote

No, it still not works because the form data can be read only one time.

If you're uploading a lot of data, such as files, you should do something like this:

const got = require('got');
const FormData = require('form-data');

const genBody = () => {
  const form = new FormData();
  form.append('A', 'B');
  return form;
};

got.post('URL', {
  body: genBody(),
  hooks: {
    beforeRetry: [
      options => {
        const form = genBody();
        options.body = form;
        options.headers['content-type'] = `multipart/form-data; boundary=${form.getBoundary()}`;
      }
    ]
  }
});

Or, if you're sending a little form (like your example) you can simply do this (because it's using a buffer…

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by sindresorhus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1513 on December 10, 2020 17:06.