Skip to content

Commit 6fc4c58

Browse files
committed
Fix php-curl-class#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.
1 parent d1e7f85 commit 6fc4c58

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/Curl/Curl.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,12 @@ interface_exists('JsonSerializable', false) &&
176176
}
177177
}
178178

179-
if (!$binary_data && (is_array($data) || is_object($data))) {
179+
if (!$binary_data &&
180+
(is_array($data) || is_object($data)) &&
181+
(
182+
!isset($this->headers['Content-Type']) ||
183+
!preg_match('/^multipart\/form-data/', $this->headers['Content-Type'])
184+
)) {
180185
$data = http_build_query($data, '', '&');
181186
}
182187

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,28 @@ public function testPutFileHandle()
552552
$this->assertEquals('image/png', $test->curl->response);
553553
}
554554

555+
public function testMultipartFormDataContentType()
556+
{
557+
// Use a PUT request instead of a POST request so the request
558+
// multipart/form-data is not automatically parsed and can be tested
559+
// against.
560+
$test = new Test();
561+
$test->curl->setHeader('Content-Type', 'multipart/form-data');
562+
$test->server('put', 'PUT', array(
563+
'foo' => 'bar',
564+
));
565+
566+
$this->assertEquals('100-continue', $test->curl->requestHeaders['Expect']);
567+
$this->assertStringStartsWith('multipart/form-data; boundary=', $test->curl->requestHeaders['Content-Type']);
568+
569+
$expected_contains = "\r\n" .
570+
'Content-Disposition: form-data; name="foo"' . "\r\n" .
571+
"\r\n" .
572+
'bar' . "\r\n" .
573+
'';
574+
$this->assertContains($expected_contains, $test->curl->response);
575+
}
576+
555577
public function testPatchRequestMethod()
556578
{
557579
$test = new Test();

0 commit comments

Comments
 (0)