mirror of
https://github.com/guzzle/guzzle.git
synced 2025-02-24 18:13:00 +01:00
Auto-detecting Content-Type of a request
- A Content-Type header will be added to a request automatically based on the path of the request if a Content-Type header was not specifically set. Pass `false` to the second parameter of EntityEnclosingRequest::setBody() to not auto-detect a value. - Removing default return value of application/octet-stream from EntityBody::getContentType()
This commit is contained in:
parent
f745ac2c81
commit
863998bbdb
@ -142,11 +142,9 @@ class EntityBody extends Stream implements EntityBodyInterface
|
||||
*/
|
||||
public function getContentType()
|
||||
{
|
||||
if (!($this->isLocal() && $this->getWrapper() == 'plainfile' && file_exists($this->getUri()))) {
|
||||
return 'application/octet-stream';
|
||||
}
|
||||
|
||||
return Mimetypes::getInstance()->fromFilename($this->getUri()) ?: 'application/octet-stream';
|
||||
return (!($this->isLocal() && $this->getWrapper() == 'plainfile' && file_exists($this->getUri())))
|
||||
? null
|
||||
: Mimetypes::getInstance()->fromFilename($this->getUri());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -47,9 +47,9 @@ interface EntityBodyInterface extends StreamInterface
|
||||
public function getContentLength();
|
||||
|
||||
/**
|
||||
* Guess the Content-Type or return the default application/octet-stream
|
||||
* Guess the Content-Type of a local stream
|
||||
*
|
||||
* @return string
|
||||
* @return string|null
|
||||
* @see http://www.php.net/manual/en/function.finfo-open.php
|
||||
*/
|
||||
public function getContentType();
|
||||
|
@ -7,6 +7,7 @@ use Guzzle\Http\EntityBodyInterface;
|
||||
use Guzzle\Http\QueryString;
|
||||
use Guzzle\Http\RedirectPlugin;
|
||||
use Guzzle\Http\Exception\RequestException;
|
||||
use Guzzle\Http\Mimetypes;
|
||||
|
||||
/**
|
||||
* HTTP request that sends an entity-body in the request message (POST, PUT, PATCH, DELETE)
|
||||
@ -78,6 +79,11 @@ class EntityEnclosingRequest extends Request implements EntityEnclosingRequestIn
|
||||
$this->body = EntityBody::factory($body);
|
||||
$this->removeHeader('Content-Length');
|
||||
|
||||
// Auto detect the Content-Type from the path of the request if possible
|
||||
if ($contentType === null && !$this->hasHeader('Content-Type')) {
|
||||
$contentType = Mimetypes::getInstance()->fromFilename($this->getPath());
|
||||
}
|
||||
|
||||
if ($contentType) {
|
||||
$this->setHeader('Content-Type', (string) $contentType);
|
||||
}
|
||||
|
@ -143,7 +143,7 @@ class EntityBodyTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
{
|
||||
// Test using a string/temp stream
|
||||
$body = EntityBody::factory('testing 123...testing 123');
|
||||
$this->assertEquals('application/octet-stream', $body->getContentType());
|
||||
$this->assertNull($body->getContentType());
|
||||
|
||||
// Use a local file
|
||||
$body = EntityBody::factory(fopen(__FILE__, 'r'));
|
||||
|
@ -41,11 +41,9 @@ class EntityEnclosingRequestTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
*/
|
||||
public function testCanSetBodyWithoutOverridingContentType()
|
||||
{
|
||||
$request = new EntityEnclosingRequest('PUT', 'http://test.com', array(
|
||||
'Content-Type' => 'application/json'
|
||||
));
|
||||
$request = new EntityEnclosingRequest('PUT', 'http://test.com', array('Content-Type' => 'foooooo'));
|
||||
$request->setBody('{"a":"b"}');
|
||||
$this->assertEquals('application/json', $request->getHeader('Content-Type'));
|
||||
$this->assertEquals('foooooo', $request->getHeader('Content-Type'));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -523,4 +521,14 @@ class EntityEnclosingRequestTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
$request->configureRedirects(false, false);
|
||||
$this->assertTrue($request->getParams()->get(RedirectPlugin::DISABLE));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Guzzle\Http\Message\EntityEnclosingRequest::setBody
|
||||
*/
|
||||
public function testSetsContentTypeWhenSettingBodyByGuessing()
|
||||
{
|
||||
$request = new EntityEnclosingRequest('PUT', 'http://test.com/foo.json');
|
||||
$request->setBody('{"a":"b"}');
|
||||
$this->assertEquals('application/json', $request->getHeader('Content-Type'));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user