Skip to content

Commit

Permalink
Make sure JSON body will default to POST
Browse files Browse the repository at this point in the history
Fixes #736
  • Loading branch information
szmarczak committed Feb 22, 2019
1 parent 4bad8f0 commit 90325bd
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
2 changes: 0 additions & 2 deletions source/normalize-arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,6 @@ const normalize = (url, options, defaults) => {

if (options.method) {
options.method = options.method.toUpperCase();
} else {
options.method = is.nullOrUndefined(options.body) ? 'GET' : 'POST';
}

if (!is.function(options.retry.retries)) {
Expand Down
4 changes: 4 additions & 0 deletions source/request-as-event-emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ module.exports = (options, input) => {
options.body = JSON.stringify(options.json);
}

if (!options.method) {
options.method = is.nullOrUndefined(options.body) ? 'GET' : 'POST';
}

// Convert buffer to stream to receive upload progress events (#322)
if (is.buffer(body)) {
options.body = toReadableStream(body);
Expand Down
5 changes: 5 additions & 0 deletions test/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,8 @@ test('content-type header is not overriden when object in options.body', async t
test('throws when form body is not a plain object or array', async t => {
await t.throwsAsync(got(`${s.url}`, {form: 'such=wow'}), TypeError);
});

test('setting JSON will default to POST', async t => {
const {headers} = await got(s.url, {json: {foo: 'bar'}});
t.is(headers.method, 'POST');
});

0 comments on commit 90325bd

Please sign in to comment.