Skip to content

Commit

Permalink
Invert the methodRewriting option
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak committed Feb 26, 2021
1 parent c1b6929 commit 51d88a0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,9 @@ Note that if a `303` is sent by the server in response to any request type (`POS
Type: `boolean`\
Default: `true`

By default, redirects will use [method rewriting](https://tools.ietf.org/html/rfc7231#section-6.4). For example, when sending a POST request and receiving a `302`, it will resend the body to the new location using the same HTTP method (`POST` in this case).
Specifies if the redirects should be [rewritten as `GET`](https://tools.ietf.org/html/rfc7231#section-6.4).

If `false`, when sending a POST request and receiving a `302`, it will resend the body to the new location using the same HTTP method (`POST` in this case).

###### allowGetBody

Expand Down
9 changes: 5 additions & 4 deletions source/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -643,10 +643,11 @@ interface PlainOptions extends URLOptions {
headers?: Headers;

/**
By default, redirects will use [method rewriting](https://tools.ietf.org/html/rfc7231#section-6.4).
For example, when sending a POST request and receiving a `302`, it will resend the body to the new location using the same HTTP method (`POST` in this case).
Specifies if the redirects should be [rewritten as `GET`](https://tools.ietf.org/html/rfc7231#section-6.4).
@default true
If `false`, when sending a POST request and receiving a `302`, it will resend the body to the new location using the same HTTP method (`POST` in this case).
@default false
*/
methodRewriting?: boolean;

Expand Down Expand Up @@ -2051,7 +2052,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
}

const shouldBeGet = statusCode === 303 && options.method !== 'GET' && options.method !== 'HEAD';
if (shouldBeGet || !options.methodRewriting) {
if (shouldBeGet || options.methodRewriting) {
// Server responded with "see other", indicating that the resource exists at another location,
// and the client should request it from that location via GET or HEAD.
options.method = 'GET';
Expand Down
2 changes: 1 addition & 1 deletion source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const defaults: InstanceDefaults = {
resolveBodyOnly: false,
maxRedirects: 10,
prefixUrl: '',
methodRewriting: true,
methodRewriting: false,
ignoreInvalidCookies: false,
context: {},
// TODO: Set this to `true` for Got 13.
Expand Down
4 changes: 2 additions & 2 deletions test/redirects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ test('body is passed on POST redirect', withServer, async (t, server, got) => {
t.is(body, 'foobar');
});

test('method rewriting can be turned off', withServer, async (t, server, got) => {
test('method rewriting', withServer, async (t, server, got) => {
server.post('/redirect', (_request, response) => {
response.writeHead(302, {
location: '/'
Expand All @@ -411,7 +411,7 @@ test('method rewriting can be turned off', withServer, async (t, server, got) =>

const {body} = await got.post('redirect', {
body: 'foobar',
methodRewriting: false,
methodRewriting: true,
hooks: {
beforeRedirect: [
options => {
Expand Down

0 comments on commit 51d88a0

Please sign in to comment.