From 1222104e54c5056b9c8f203f851bdbae449aaae2 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 26 Mar 2017 16:13:44 +0200 Subject: [PATCH] Do not use json request body (#313) Build a query encoded request strings and set proper headers. --- src/Mailgun/Api/HttpApi.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/Mailgun/Api/HttpApi.php b/src/Mailgun/Api/HttpApi.php index 467689c..e7fcb39 100644 --- a/src/Mailgun/Api/HttpApi.php +++ b/src/Mailgun/Api/HttpApi.php @@ -139,7 +139,9 @@ abstract class HttpApi */ protected function httpPost($path, array $parameters = [], array $requestHeaders = []) { - return $this->httpPostRaw($path, $this->createJsonBody($parameters), $requestHeaders); + $requestHeaders['Content-Type'] = 'application/x-www-form-urlencoded'; + + return $this->httpPostRaw($path, $this->createRequestBody($parameters), $requestHeaders); } /** @@ -175,9 +177,11 @@ abstract class HttpApi */ protected function httpPut($path, array $parameters = [], array $requestHeaders = []) { + $requestHeaders['Content-Type'] = 'application/x-www-form-urlencoded'; + try { $response = $this->httpClient->sendRequest( - $this->requestBuilder->create('PUT', $path, $requestHeaders, $this->createJsonBody($parameters)) + $this->requestBuilder->create('PUT', $path, $requestHeaders, $this->createRequestBody($parameters)) ); } catch (HttplugException\NetworkException $e) { throw HttpServerException::networkError($e); @@ -197,9 +201,11 @@ abstract class HttpApi */ protected function httpDelete($path, array $parameters = [], array $requestHeaders = []) { + $requestHeaders['Content-Type'] = 'application/x-www-form-urlencoded'; + try { $response = $this->httpClient->sendRequest( - $this->requestBuilder->create('DELETE', $path, $requestHeaders, $this->createJsonBody($parameters)) + $this->requestBuilder->create('DELETE', $path, $requestHeaders, $this->createRequestBody($parameters)) ); } catch (HttplugException\NetworkException $e) { throw HttpServerException::networkError($e); @@ -215,8 +221,8 @@ abstract class HttpApi * * @return null|string */ - protected function createJsonBody(array $parameters) + protected function createRequestBody(array $parameters) { - return (count($parameters) === 0) ? null : json_encode($parameters, empty($parameters) ? JSON_FORCE_OBJECT : 0); + return (count($parameters) === 0) ? null : http_build_query($parameters); } }