Do not use json request body (#313)

Build a query encoded request strings and set proper headers.
This commit is contained in:
Tobias Nyholm 2017-03-26 16:13:44 +02:00 committed by GitHub
parent 9e19f12a3d
commit 1222104e54

View File

@ -139,7 +139,9 @@ abstract class HttpApi
*/ */
protected function httpPost($path, array $parameters = [], array $requestHeaders = []) 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 = []) protected function httpPut($path, array $parameters = [], array $requestHeaders = [])
{ {
$requestHeaders['Content-Type'] = 'application/x-www-form-urlencoded';
try { try {
$response = $this->httpClient->sendRequest( $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) { } catch (HttplugException\NetworkException $e) {
throw HttpServerException::networkError($e); throw HttpServerException::networkError($e);
@ -197,9 +201,11 @@ abstract class HttpApi
*/ */
protected function httpDelete($path, array $parameters = [], array $requestHeaders = []) protected function httpDelete($path, array $parameters = [], array $requestHeaders = [])
{ {
$requestHeaders['Content-Type'] = 'application/x-www-form-urlencoded';
try { try {
$response = $this->httpClient->sendRequest( $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) { } catch (HttplugException\NetworkException $e) {
throw HttpServerException::networkError($e); throw HttpServerException::networkError($e);
@ -215,8 +221,8 @@ abstract class HttpApi
* *
* @return null|string * @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);
} }
} }