From 6fc4c5824b5f991c13a1cb232728e80550ffb340 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Fri, 7 Sep 2018 23:53:02 -0700 Subject: [PATCH] Fix #545: Allow fileless requests to use Content-Type: multipart/form-data. This change allows making requests that don't contain files to use Content-Type: multipart/form-data. Previously, requests with array data not specifying any files were sent using Content-Type: application/x-www-form-urlencoded. --- src/Curl/Curl.php | 7 ++++++- tests/PHPCurlClass/PHPCurlClassTest.php | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 2e6d897..e0bf08b 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -176,7 +176,12 @@ class Curl } } - if (!$binary_data && (is_array($data) || is_object($data))) { + if (!$binary_data && + (is_array($data) || is_object($data)) && + ( + !isset($this->headers['Content-Type']) || + !preg_match('/^multipart\/form-data/', $this->headers['Content-Type']) + )) { $data = http_build_query($data, '', '&'); } diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index db5b197..66db23c 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -552,6 +552,28 @@ class CurlTest extends \PHPUnit\Framework\TestCase $this->assertEquals('image/png', $test->curl->response); } + public function testMultipartFormDataContentType() + { + // Use a PUT request instead of a POST request so the request + // multipart/form-data is not automatically parsed and can be tested + // against. + $test = new Test(); + $test->curl->setHeader('Content-Type', 'multipart/form-data'); + $test->server('put', 'PUT', array( + 'foo' => 'bar', + )); + + $this->assertEquals('100-continue', $test->curl->requestHeaders['Expect']); + $this->assertStringStartsWith('multipart/form-data; boundary=', $test->curl->requestHeaders['Content-Type']); + + $expected_contains = "\r\n" . + 'Content-Disposition: form-data; name="foo"' . "\r\n" . + "\r\n" . + 'bar' . "\r\n" . + ''; + $this->assertContains($expected_contains, $test->curl->response); + } + public function testPatchRequestMethod() { $test = new Test();