From 646e24aefec139e5d104e6804a6630a7ae7339bd Mon Sep 17 00:00:00 2001 From: Sam Sullivan Date: Mon, 13 Jan 2014 23:21:04 -0600 Subject: [PATCH] Check if $body is traversable before recursively sanitizing (allows for raw JSON body) Add testRawPost() --- lib/Unirest/Unirest.php | 10 +++++++--- test/Unirest/UnirestTest.php | 13 +++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/Unirest/Unirest.php b/lib/Unirest/Unirest.php index ef3fbf2..0edaf91 100644 --- a/lib/Unirest/Unirest.php +++ b/lib/Unirest/Unirest.php @@ -171,9 +171,13 @@ private static function request($httpMethod, $url, $body = NULL, $headers = arra $ch = curl_init(); if ($httpMethod != HttpMethod::GET) { - curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, $httpMethod); - Unirest::http_build_query_for_curl($body, $postBody); - curl_setopt ($ch, CURLOPT_POSTFIELDS, $postBody); + curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, $httpMethod); + if( is_array($body) || $body instanceof Traversable ) { + Unirest::http_build_query_for_curl($body, $postBody); + curl_setopt ($ch, CURLOPT_POSTFIELDS, $postBody); + } else { + curl_setopt ($ch, CURLOPT_POSTFIELDS, $body); + } } else if (is_array($body)) { if (strpos($url,'?') !== false) { $url .= "&"; diff --git a/test/Unirest/UnirestTest.php b/test/Unirest/UnirestTest.php index e6eca2b..faffaed 100644 --- a/test/Unirest/UnirestTest.php +++ b/test/Unirest/UnirestTest.php @@ -90,6 +90,19 @@ public function testPostWithDots() $this->assertEqual("thefosk", $form->nick); } + public function testRawPost() + { + $response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), + json_encode(array( + "author" => "Sam Sullivan" + ))); + + $this->assertEqual(200, $response->code); + + $json = $response->body->json; + $this->assertEqual("Sam Sullivan", $json->author); + } + public function testUpload() { $response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), array(