Skip to content

Commit

Permalink
Fix redirect policy
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Nov 20, 2019
1 parent d9935ef commit 00498c2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 5 additions & 3 deletions docs/follow-redirects.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ Automatic following can be customized or disabled (using a limit of `0`) using `

## Redirect Policy

Redirects to the same host will clone the request and apply the redirect URI.
If the response status code is `300`, `301`, `302`, or `303`, the request body is removed and the request method changed to `GET`.
Redirects to other hosts will always follow with an empty `Request` using the `GET` method.
The `FollowRedirects` interceptor will only follow redirects with a `GET` method.
If another request method is used and a `307` or `308` response is received, the response will be returned as is, so another interceptor or the application can take care of it.
Cross-origin redirects will be attempted without any headers set, so any application headers will be discarded.
If `HttpClientBuilder` is used to configure the client, the `FollowRedirects` interceptor is the outermost interceptor, so any headers set by interceptors will still be present in the response.
It is therefore recommended to set headers via interceptors instead of directly in the request.

## Examining the Redirect Chain

Expand Down
2 changes: 1 addition & 1 deletion src/Interceptor/FollowRedirects.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ private function getRedirectUri(Response $response): ?PsrUri
$request = $response->getRequest();
$method = $request->getMethod();

if ($method !== 'GET') {
if ($method !== 'GET' && \in_array($status, [307, 308], true)) {
return null;
}

Expand Down

0 comments on commit 00498c2

Please sign in to comment.