From 97cd8b6422cbf2f5bd3040b4d70f1629437b521c Mon Sep 17 00:00:00 2001 From: datagutten Date: Sat, 5 Oct 2019 19:11:53 +0200 Subject: [PATCH 1/2] Add support for sending POST with multipart/form-data --- library/Requests/Transport/cURL.php | 7 ++++++- library/Requests/Transport/fsockopen.php | 15 ++++++++++++++- tests/Transport/Base.php | 11 +++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/library/Requests/Transport/cURL.php b/library/Requests/Transport/cURL.php index 4429edb64..0e1794069 100644 --- a/library/Requests/Transport/cURL.php +++ b/library/Requests/Transport/cURL.php @@ -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); @@ -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, '&'); } } diff --git a/library/Requests/Transport/fsockopen.php b/library/Requests/Transport/fsockopen.php index c7c61d3b1..876e8583c 100644 --- a/library/Requests/Transport/fsockopen.php +++ b/library/Requests/Transport/fsockopen.php @@ -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; diff --git a/tests/Transport/Base.php b/tests/Transport/Base.php index ebd6ca98a..6c5c1425e 100644 --- a/tests/Transport/Base.php +++ b/tests/Transport/Base.php @@ -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()); From 99e8e8682818046abec60f174c3b6fec66e730c8 Mon Sep 17 00:00:00 2001 From: datagutten Date: Sat, 5 Oct 2019 19:22:20 +0200 Subject: [PATCH 2/2] Indent with tabs --- library/Requests/Transport/cURL.php | 12 +++++----- library/Requests/Transport/fsockopen.php | 28 ++++++++++++------------ tests/Transport/Base.php | 18 +++++++-------- 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/library/Requests/Transport/cURL.php b/library/Requests/Transport/cURL.php index 0e1794069..d252b2b89 100644 --- a/library/Requests/Transport/cURL.php +++ b/library/Requests/Transport/cURL.php @@ -313,11 +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; + if(isset($headers['Content-Type']) && $headers['Content-Type']==='multipart/form-data') { + $is_multipart_form = true; + } + else + $is_multipart_form = false; $headers = Requests::flatten($headers); @@ -328,7 +328,7 @@ protected function setup_handle($url, $headers, $data, $options) { $url = self::format_get($url, $data); $data = ''; } - elseif (!is_string($data) && !$is_multipart_form) { + elseif (!is_string($data) && !$is_multipart_form) { $data = http_build_query($data, null, '&'); } } diff --git a/library/Requests/Transport/fsockopen.php b/library/Requests/Transport/fsockopen.php index 876e8583c..d6966d7d8 100644 --- a/library/Requests/Transport/fsockopen.php +++ b/library/Requests/Transport/fsockopen.php @@ -154,20 +154,20 @@ public function request($url, $headers = array(), $data = array(), $options = ar if ($options['type'] !== Requests::TRACE) { if (is_array($data)) { - 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, '&'); + 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; diff --git a/tests/Transport/Base.php b/tests/Transport/Base.php index 6c5c1425e..f8484e4fe 100644 --- a/tests/Transport/Base.php +++ b/tests/Transport/Base.php @@ -204,15 +204,15 @@ public function testPOSTWithNestedData() { } 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']); - } + $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';