Skip to content

Commit

Permalink
Set content length for empty requests
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Nov 15, 2019
1 parent 13fc6d9 commit 3b7f074
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions src/Connections/RequestsConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
use Fortifi\Api\Core\IApiRequest;
use Fortifi\Api\Core\IApiResult;
use Requests;
use Requests_Hooks;
use function in_array;
use function array_filter;

class RequestsConnection extends AbstractConnection
{
Expand All @@ -34,22 +33,18 @@ public function load(IApiRequest $request)

try
{
$hooker = new Requests_Hooks();
$hooker->register(
'requests.before_request',
function ($url, &$headers, $data, $method) {
if(in_array($method, [Requests::PATCH, Requests::PUT, Requests::DELETE]) && empty($data))
{
// Prevent 411 errors from some servers
$headers['Content-Length'] = 0;
}
}
);
$headers = $this->_buildHeaders($req);
$data = $this->_buildData($req);
if(empty(array_filter($data)))
{
// Prevent 411 errors from some servers
$headers['Content-Length'] = 0;
}

$response = Requests::request(
$req->getUrl(),
$this->_buildHeaders($req),
$this->_buildData($req),
$headers,
$data,
$req->getMethod(),
array_merge(['timeout' => $this->_timeout], (array)$req->getOptions())
);
Expand Down

0 comments on commit 3b7f074

Please sign in to comment.