Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for sending POST with multipart/form-data #371

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion library/Requests/Transport/cURL.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,11 @@ protected function setup_handle($url, $headers, $data, $options) {
if ( ! isset( $headers['Connection'] ) ) {
$headers['Connection'] = 'close';
}
if(isset($headers['Content-Type']) && $headers['Content-Type']==='multipart/form-data') {
$is_multipart_form = true;
}
else
$is_multipart_form = false;

$headers = Requests::flatten($headers);

Expand All @@ -323,7 +328,7 @@ protected function setup_handle($url, $headers, $data, $options) {
$url = self::format_get($url, $data);
$data = '';
}
elseif (!is_string($data)) {
elseif (!is_string($data) && !$is_multipart_form) {
$data = http_build_query($data, null, '&');
}
}
Expand Down
15 changes: 14 additions & 1 deletion library/Requests/Transport/fsockopen.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,20 @@ public function request($url, $headers = array(), $data = array(), $options = ar

if ($options['type'] !== Requests::TRACE) {
if (is_array($data)) {
$request_body = http_build_query($data, null, '&');
if (isset($case_insensitive_headers['Content-Type']) && $case_insensitive_headers['Content-Type']==='multipart/form-data')
{
$boundary = '------------------------'.substr(md5(rand()), 0, 16);
$headers['Content-Type'] = sprintf('multipart/form-data; boundary=%s', $boundary);

foreach ($data as $key=>$value)
{
$request_body .= '--'.$boundary."\r\n";
$request_body .= sprintf("Content-Disposition: form-data; name=\"%s\"\r\n\r\n%s\r\n", $key, $value);
}
$request_body .= '--'.$boundary."--\r\n";
}
else
$request_body = http_build_query($data, null, '&');
}
else {
$request_body = $data;
Expand Down
11 changes: 11 additions & 0 deletions tests/Transport/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,17 @@ public function testPOSTWithNestedData() {
$this->assertEquals(array('test' => 'true', 'test2[test3]' => 'test', 'test2[test4]' => 'test-too'), $result['form']);
}

public function testPOSTWithMultipartData() {
$data = array(
'test' => 'true',
'test2' => 'test',
);
$request = Requests::post('http://httpbin.org/post', array('Content-Type'=>'multipart/form-data'), $data, $this->getOptions());
$this->assertEquals(200, $request->status_code);
$result = json_decode($request->body, true);
$this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['form']);
}

public function testRawPUT() {
$data = 'test';
$request = Requests::put(httpbin('/put'), array(), $data, $this->getOptions());
Expand Down