diff --git a/lib/Cake/Network/CakeRequest.php b/lib/Cake/Network/CakeRequest.php index cd9e893bd..4ea520280 100644 --- a/lib/Cake/Network/CakeRequest.php +++ b/lib/Cake/Network/CakeRequest.php @@ -165,7 +165,7 @@ class CakeRequest implements ArrayAccess { if ($_POST) { $this->data = $_POST; } elseif (($this->is('put') || $this->is('delete')) && - strpos(env('CONTENT_TYPE'), 'application/x-www-form-urlencoded') === 0 + strpos($this->contentType(), 'application/x-www-form-urlencoded') === 0 ) { $data = $this->_readInput(); parse_str($data, $this->data); @@ -394,6 +394,19 @@ class CakeRequest implements ArrayAccess { } } +/** + * Get the content type used in this request. + * + * @return string + */ + public function contentType() { + $type = env('CONTENT_TYPE'); + if ($type) { + return $type; + } + return env('HTTP_CONTENT_TYPE'); + } + /** * Get the IP the client is using, or says they are using. * diff --git a/lib/Cake/Test/Case/Network/CakeRequestTest.php b/lib/Cake/Test/Case/Network/CakeRequestTest.php index 684f4286b..e1c1d3598 100644 --- a/lib/Cake/Test/Case/Network/CakeRequestTest.php +++ b/lib/Cake/Test/Case/Network/CakeRequestTest.php @@ -146,6 +146,21 @@ class CakeRequestTest extends CakeTestCase { $this->assertFalse(isset($request->query['one'])); } +/** + * Test the content type method. + * + * @return void + */ + public function testContentType() { + $_SERVER['HTTP_CONTENT_TYPE'] = 'application/json'; + $request = new CakeRequest('/', false); + $this->assertEquals('application/json', $request->contentType()); + + $_SERVER['CONTENT_TYPE'] = 'application/xml'; + $request = new CakeRequest('/', false); + $this->assertEquals('application/xml', $request->contentType(), 'prefer non http header.'); + } + /** * Test construction *