Configure Guzzle to follow redirects and keep POST data
Published in
1 min readSep 15, 2020
Sometimes you want to configure your Guzzle Client to follow redirects when doing a request, for example when you redirect an http to an https URL. In case of a POST request, Guzzle will lose the POST data.
You have to tell Guzzle to use “strict RFC compliant redirects” which will mean a redirected POST request will result in another POST request instead of the default behavior that will result in a GET request.
From the documentation:
Pass an associative array containing the 'max' key to specify the maximum number of redirects and optionally provide a 'strict' key value to specify whether or not to use strict RFC compliant redirects (meaning redirect POST requests with POST requests vs. doing what most browsers do which is redirect POST requests with GET requests).
An example will look like this:
new GuzzleClient([
'base_uri' => 'http://...',
'allow_redirects' => ['strict' => true],
]);