form-data / form-data

A module to create readable `"multipart/form-data"` streams. Can be used to submit forms and file uploads to other web applications.
https://www.npmjs.com/form-data
MIT License
2.27k stars 289 forks source link

got with form-data doesn't appear to work #525

Open octoberspice opened 2 years ago

octoberspice commented 2 years ago

Using got 12.0.1, I get the error "RequestError: The body option must be a stream.Readable, string or Buffer" on the following code

    var fd = require('formdata-node').FormData;
    var form = new fd();
    form.set('t', text);
    var body = await got.post('<my url>', {
        "body": form,
        headers: {
            "Content-Type": 'multipart/form-data',
            "Authorization": "Basic ..."
        }
    }).json();

Documentation suggestions that this should work https://github.com/sindresorhus/got/blob/HEAD/documentation/2-options.md , Not sure what I am missing.

Nivg commented 8 months ago

@octoberspice I know it's an old issue, but if someone is reading this and struggle with this as well. Seems like adding form: true to the options object solve this.

import { FormData } from "formdata-node";
import got from "got";

const form = new FormData();
form.set("text", "SOME_TEXT");
const data = await got.post("MY_URL", {
  body: form,
  form: true,
  headers: {
    "Content-Type": "multipart/form-data",
    Authorization: "Basic ...",
  },
});

I did not found mention of this requirement in the documentation...In addition, seems like using .json() does not work with that combination.

octet-stream commented 5 months ago

@octoberspice @Nivg This issue seem to be related to another package, and not to this one. Please refer to the documentation of that package instead. Try latest version of got and formdata-node and if you have any issue - open a new one in my repo.