From 13fc6d9cbffdcb2905b3ce4d3187e16ec3a6a9cc Mon Sep 17 00:00:00 2001 From: Brooke Bryan Date: Fri, 15 Nov 2019 15:00:38 +0000 Subject: [PATCH] Put, Patch and Delete should set a content length --- src/Connections/RequestsConnection.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Connections/RequestsConnection.php b/src/Connections/RequestsConnection.php index 0e4f46c..534b9df 100644 --- a/src/Connections/RequestsConnection.php +++ b/src/Connections/RequestsConnection.php @@ -8,6 +8,9 @@ use Fortifi\Api\Core\Exceptions\Server\ServiceUnavailableException; use Fortifi\Api\Core\IApiRequest; use Fortifi\Api\Core\IApiResult; +use Requests; +use Requests_Hooks; +use function in_array; class RequestsConnection extends AbstractConnection { @@ -31,7 +34,19 @@ public function load(IApiRequest $request) try { - $response = \Requests::request( + $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; + } + } + ); + + $response = Requests::request( $req->getUrl(), $this->_buildHeaders($req), $this->_buildData($req),