Skip to content

Commit b65ddd8

Browse files
committed
fixed bug that causes improper redirect logic after POST
1 parent bb06819 commit b65ddd8

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/Client.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,16 @@ public function request($method, $uri, $params = array(), $headers = array(), $c
4545
curl_setopt($ch, CURLOPT_URL, $uri);
4646

4747
$post_data = is_array($params) ? http_build_query($params) : $params;
48+
49+
if ($method == 'POST') {
50+
curl_setopt($ch, CURLOPT_POST, 1);
51+
} else {
52+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
53+
}
54+
4855
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
4956
}
5057

51-
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
52-
5358
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getCombinedHeaders($headers));
5459
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
5560
curl_setopt($ch, CURLOPT_HEADER, 0);

tests/HttpBinTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,17 @@ public function test_redirect_nofollow()
8383

8484
$this->assertEquals(302, $response->status);
8585
}
86+
87+
// https://github.com/Kong/insomnia/issues/227
88+
public function test_redirect_switch_to_get()
89+
{
90+
$client = new Client();
91+
92+
$response = $client->post('https://httpbin.org/redirect-to?url=https://www.google.com/&status_code=301');
93+
94+
// if CURLOPT_CUSTOMREQUEST was set, then redirect would follow from POST -> POST, rather than POST -> GET
95+
// POST to google index gives 405
96+
// <p>The request method <code>POST</code> is inappropriate for the URL <code>/</code>. <ins>That’s all we know.</ins>
97+
$this->assertEquals(200, $response->status);
98+
}
8699
}

0 commit comments

Comments
 (0)