Skip to content

Commit e049e94

Browse files
authored
Improve docs and disable method rewriting on 307 and 308 (#2145)
1 parent 26fffe9 commit e049e94

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

documentation/2-options.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ Defines if redirect responses should be followed automatically.
692692

693693
#### **Note:**
694694
> - If a `303` is sent by the server in response to any request type (POST, DELETE, etc.), Got will request the resource pointed to in the location header via GET.\
695-
> This is in accordance with the [specification](https://tools.ietf.org/html/rfc7231#section-6.4.4).
695+
> This is in accordance with the [specification](https://tools.ietf.org/html/rfc7231#section-6.4.4). You can optionally turn on this behavior also for other redirect codes - see [`methodRewriting`](#methodrewriting).
696696
697697
```js
698698
import got from 'got';
@@ -936,9 +936,12 @@ Optionally overrides the value of [`--max-http-header-size`](https://nodejs.org/
936936
**Type: `boolean`**\
937937
**Default: `false`**
938938

939-
By default, requests will not use [method rewriting](https://datatracker.ietf.org/doc/html/rfc7231#section-6.4).
939+
Specifies if the HTTP request method should be [rewritten as `GET`](https://tools.ietf.org/html/rfc7231#section-6.4) on redirects.
940940

941-
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). To rewrite the request as `GET`, set this option to `true`.
941+
As the [specification](https://tools.ietf.org/html/rfc7231#section-6.4) prefers to rewrite the HTTP method only on `303` responses, this is Got's default behavior. Setting `methodRewriting` to `true` will also rewrite `301` and `302` responses, as allowed by the spec. This is the behavior followed by `curl` and browsers.
942+
943+
**Note:**
944+
> - Got never performs method rewriting on `307` and `308` responses, as this is [explicitly prohibited by the specification](https://www.rfc-editor.org/rfc/rfc7231#section-6.4.7).
942945
943946
### `enableUnixSockets`
944947

source/core/index.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -731,10 +731,11 @@ export default class Request extends Duplex implements RequestEvents<Request> {
731731

732732
const updatedOptions = new Options(undefined, undefined, this.options);
733733

734-
const shouldBeGet = statusCode === 303 && updatedOptions.method !== 'GET' && updatedOptions.method !== 'HEAD';
735-
if (shouldBeGet || updatedOptions.methodRewriting) {
736-
// Server responded with "see other", indicating that the resource exists at another location,
737-
// and the client should request it from that location via GET or HEAD.
734+
const serverRequestedGet = statusCode === 303 && updatedOptions.method !== 'GET' && updatedOptions.method !== 'HEAD';
735+
const canRewrite = statusCode !== 307 && statusCode !== 308;
736+
const userRequestedGet = updatedOptions.methodRewriting && canRewrite;
737+
738+
if (serverRequestedGet || userRequestedGet) {
738739
updatedOptions.method = 'GET';
739740

740741
updatedOptions.body = undefined;

source/core/options.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1764,7 +1764,7 @@ export default class Options {
17641764
Defines if redirect responses should be followed automatically.
17651765
17661766
Note that if a `303` is sent by the server in response to any request type (`POST`, `DELETE`, etc.), Got will automatically request the resource pointed to in the location header via `GET`.
1767-
This is in accordance with [the spec](https://tools.ietf.org/html/rfc7231#section-6.4.4).
1767+
This is in accordance with [the spec](https://tools.ietf.org/html/rfc7231#section-6.4.4). You can optionally turn on this behavior also for other redirect codes - see `methodRewriting`.
17681768
17691769
@default true
17701770
*/
@@ -1955,9 +1955,12 @@ export default class Options {
19551955
}
19561956

19571957
/**
1958-
Specifies if the redirects should be [rewritten as `GET`](https://tools.ietf.org/html/rfc7231#section-6.4).
1958+
Specifies if the HTTP request method should be [rewritten as `GET`](https://tools.ietf.org/html/rfc7231#section-6.4) on redirects.
19591959
1960-
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).
1960+
As the [specification](https://tools.ietf.org/html/rfc7231#section-6.4) prefers to rewrite the HTTP method only on `303` responses, this is Got's default behavior.
1961+
Setting `methodRewriting` to `true` will also rewrite `301` and `302` responses, as allowed by the spec. This is the behavior followed by `curl` and browsers.
1962+
1963+
__Note__: Got never performs method rewriting on `307` and `308` responses, as this is [explicitly prohibited by the specification](https://www.rfc-editor.org/rfc/rfc7231#section-6.4.7).
19611964
19621965
@default false
19631966
*/

test/redirects.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -459,11 +459,20 @@ test('method rewriting', withServer, async (t, server, got) => {
459459
});
460460
response.end();
461461
});
462-
463462
server.get('/', (_request, response) => {
464463
response.end();
465464
});
466465

466+
server.post('/temporaryRedirect', (_request, response) => {
467+
response.writeHead(307, {
468+
location: '/',
469+
});
470+
response.end();
471+
});
472+
server.post('/', (request, response) => {
473+
request.pipe(response);
474+
});
475+
467476
const {body} = await got.post('redirect', {
468477
body: 'foobar',
469478
methodRewriting: true,
@@ -477,6 +486,21 @@ test('method rewriting', withServer, async (t, server, got) => {
477486
});
478487

479488
t.is(body, '');
489+
490+
// Do not rewrite method on 307 or 308
491+
const {body: temporaryRedirectBody} = await got.post('temporaryRedirect', {
492+
body: 'foobar',
493+
methodRewriting: true,
494+
hooks: {
495+
beforeRedirect: [
496+
options => {
497+
t.is(options.body, 'foobar');
498+
},
499+
],
500+
},
501+
});
502+
503+
t.is(temporaryRedirectBody, 'foobar');
480504
});
481505

482506
test('clears username and password when redirecting to a different hostname', withServer, async (t, server, got) => {

0 commit comments

Comments
 (0)