diff --git a/readme.md b/readme.md index 7fe4c2422..642c2912e 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/source/core/index.ts b/source/core/index.ts index 75e231395..d3b635c99 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -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; @@ -2051,7 +2052,7 @@ export default class Request extends Duplex implements RequestEvents { } 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'; diff --git a/source/index.ts b/source/index.ts index 9e9629ca3..42dce9311 100644 --- a/source/index.ts +++ b/source/index.ts @@ -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. diff --git a/test/redirects.ts b/test/redirects.ts index 8286b8d33..cfe5088d9 100644 --- a/test/redirects.ts +++ b/test/redirects.ts @@ -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: '/' @@ -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 => {