Skip to content

Commit f06bc47

Browse files
committed
Request body can be send with any method except GET, HEAD and TRACE.
1 parent 4c908c9 commit f06bc47

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change Log
22

3+
## Unreleased
4+
5+
### Changed
6+
7+
- Request body can be send with any method except GET, HEAD and TRACE.
8+
39
## 1.4.2 - 2016-06-14
410

511
### Added

src/Client.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,15 @@ private function createCurlOptions(RequestInterface $request, ResponseBuilder $r
202202
$options[CURLOPT_HTTP_VERSION] = $this->getProtocolVersion($request->getProtocolVersion());
203203
$options[CURLOPT_URL] = (string) $request->getUri();
204204

205-
if (in_array($request->getMethod(), ['OPTIONS', 'POST', 'PUT'], true)) {
206-
// cURL allows request body only for these methods.
205+
/*
206+
* Add body to request. Some HTTP methods can not have payload:
207+
*
208+
* - GET — cURL will automatically change method to PUT or POST if we set CURLOPT_UPLOAD or
209+
* CURLOPT_POSTFIELDS.
210+
* - HEAD — cURL treats HEAD as GET request with a same restrictions.
211+
* - TRACE — According to RFC7231: a client MUST NOT send a message body in a TRACE request.
212+
*/
213+
if (!in_array($request->getMethod(), ['GET', 'HEAD', 'TRACE'], true)) {
207214
$body = $request->getBody();
208215
$bodySize = $body->getSize();
209216
if ($bodySize !== 0) {
@@ -225,6 +232,7 @@ private function createCurlOptions(RequestInterface $request, ResponseBuilder $r
225232
}
226233

227234
if ($request->getMethod() === 'HEAD') {
235+
// This will set HTTP method to "HEAD".
228236
$options[CURLOPT_NOBODY] = true;
229237
} elseif ($request->getMethod() !== 'GET') {
230238
// GET is a default method. Other methods should be specified explicitly.

tests/HttpAsyncClientTestCase.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function testAsyncSendRequest($method, $uri, array $headers, $body)
1717
if (defined('HHVM_VERSION')) {
1818
static::markTestSkipped('This test can not run under HHVM');
1919
}
20-
if (null !== $body && !in_array($method, ['OPTIONS', 'POST', 'PUT'], true)) {
20+
if (null !== $body && in_array($method, ['GET', 'HEAD', 'TRACE'], true)) {
2121
static::markTestSkipped('cURL can not send body using ' . $method);
2222
}
2323
parent::testAsyncSendRequest(

tests/HttpClientTestCase.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function testSendRequest($method, $uri, array $headers, $body)
2626
if (defined('HHVM_VERSION')) {
2727
static::markTestSkipped('This test can not run under HHVM');
2828
}
29-
if (null !== $body && !in_array($method, ['OPTIONS', 'POST', 'PUT'], true)) {
29+
if (null !== $body && in_array($method, ['GET', 'HEAD', 'TRACE'], true)) {
3030
static::markTestSkipped('cURL can not send body using ' . $method);
3131
}
3232
parent::testSendRequest(

0 commit comments

Comments
 (0)