Repro code:
const got = require('got');
const gotOpts = {
body: {foo: 'bar'},
json: true,
method: 'POST',
timeout: 500,
url: 'http://localhost:8080/magnet'
};
got(gotOpts)
.then(response => console.log(response))
.catch(err => console.error(err.stack));
The server listening on http://localhost:8080/magnet doesn't show anything is sent, whereas curl works just fine:
$ curl http://localhost:8080/magnet -XPOST
{"error":"RPC body missing properties: message, payload, endpoint"}
(the error message is expected here - the request completes fine)
Wireshark confirms nothing is actually being sent.
Sidenote: It'd be nice to have some more definitive examples of how to use got when performing non-GET requests. The examples for POST only really show a few edge-case use-cases; otherwise, there are a number of vague ways you can use Got's API to make POST/PUT/etc. requests with body data.
Okay, I see what the problem is: got expects the first parameter to be a URL string.
url: parameter at all on the options object? Seems redundant, and the fact it overrides the mandatory got(url, ...) argument seems like code smell.Either you specify an URL as the first argument and options as the second argument, or you only specify an options object as the first argument with the URL as URL parts (protocol, host, path). The first string argument is just a convenience shortcut. I do agree we should have better validation and docs on this.
tl;dr You can't specify a url option if the first argument is an options argument. Not sure whether we should support this or not. If not, we should at least throw a helpful error.
I'll try to find some time to add a good POST usage example to the readme.
Oh, the url parts part wasn't clear in the docs (or maybe I missed it). It'd be nice if it also supported just a string.
I don't think we should support an url option, but I do think we should detect the mistake and throw a useful error.
I don't think we should support an url option, but I do think we should detect the mistake and throw a useful error.
It already does, but it isn't very useful.
TypeError [ERR_INVALID_URL]: Invalid URL: http:
at onParseError (internal/url.js:234:17)
at parse (internal/url.js:243:3)
at new URL (internal/url.js:318:5)
at new URL (internal/url.js:316:14)
at module.exports (/home/szymon/Desktop/got-master/source/request-as-event-emitter.js:19:38)
at PCancelable (/home/szymon/Desktop/got-master/source/as-promise.js:13:19)
at PCancelable._promise.Promise (/home/szymon/Desktop/got-master/node_modules/p-cancelable/index.js:32:11)
at new Promise (<anonymous>)
at new PCancelable (/home/szymon/Desktop/got-master/node_modules/p-cancelable/index.js:29:19)
at module.exports.options (/home/szymon/Desktop/got-master/source/as-promise.js:12:21)
Tested on master.
Now it throws Error: Parameter 'url' is not an option. Use got(url, options) :tada:
Most helpful comment
Now it throws
Error: Parameter 'url' is not an option. Use got(url, options):tada: