Set the body of the request
string|resource|EntityBodyInterface $body Body to use in the entity body of the request:
string $contentType Content-Type to set. Leave null to use an existing: Content-Type or to guess the Content-Type
bool $tryChunkedTransfer Try to use chunked Transfer-Encoding:
EntityEnclosingRequestInterface
RequestException if the protocol is < 1.1 and Content-Length can not be determined
Overrides EntityEnclosingRequestInterface::setBody
public function setBody($body, $contentType = null, $tryChunkedTransfer = false) {
$this->body = EntityBody::factory($body);
$this
->removeHeader('Content-Length');
if ($contentType) {
$this
->setHeader('Content-Type', (string) $contentType);
}
// Always add the Expect 100-Continue header if the body cannot be rewound. This helps with redirects.
if (!$this->body
->isSeekable() && $this->expectCutoff !== false) {
$this
->setHeader('Expect', '100-Continue');
}
if ($tryChunkedTransfer) {
$this
->setHeader('Transfer-Encoding', 'chunked');
}
else {
$this
->removeHeader('Transfer-Encoding');
// Set the Content-Length header if it can be determined
$size = $this->body
->getContentLength();
if ($size !== null && $size !== false) {
$this
->setHeader('Content-Length', $size);
if ($size > $this->expectCutoff) {
$this
->setHeader('Expect', '100-Continue');
}
}
elseif ('1.1' == $this->protocolVersion) {
$this
->setHeader('Transfer-Encoding', 'chunked');
}
else {
throw new RequestException('Cannot determine Content-Length and cannot use chunked Transfer-Encoding when using HTTP/1.0');
}
}
return $this;
}