node.jsnode.js-got

Undocumented behaviour related with body, json and form parameters in got Node module


Got module documentation states that:

If body is specified, then the json or form option cannot be used.

What does "cannot" mean in this context? The code explodes with an exception raise? Or one of them (either body or from/json) is ignored?

What about the same between json and form? What happens if I try to use both at the same time?


Solution

  • As you can tell with a quick test, e.g.:

    import got from "got";
    
    (async () => {
        try {
            await got.post("http://localhost:3000", {
                body: "foo=bar",
                json: { foo: "bar" },
            });
    
        } catch (err) {
            console.error(err);
            process.exit(1);
        }
    })();
    

    got yells at you if you provide more than one of body, form or json:

    RequestError: Expected value which is `undefined`, received value of type `string`.
    

    this is a bit cryptic, but there's a hint further down the traceback that it's unhappy about the JSON:

        at Options.set json [as json] (file:///path/to/node_modules/got/dist/source/core/options.js:656:20)