Skip to content

Commit

Permalink
Merge pull request #132 from rumeau/master
Browse files Browse the repository at this point in the history
Fix invalid JSON error when parsing empty body rest response
  • Loading branch information
delatbabel committed Jul 15, 2016
2 parents d3fca21 + ce4c0b2 commit ec7fc86
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ php:
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm

before_script:
Expand Down
5 changes: 4 additions & 1 deletion src/Message/AbstractRestRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ function ($event) {
try {
$httpRequest->getCurlOptions()->set(CURLOPT_SSLVERSION, 6); // CURL_SSLVERSION_TLSv1_2 for libcurl < 7.35
$httpResponse = $httpRequest->send();
return $this->response = $this->createResponse($httpResponse->json(), $httpResponse->getStatusCode());
// Empty response body should be parsed also as and empty array
$body = $httpResponse->getBody(true);
$jsonToArrayResponse = !empty($body) ? $httpResponse->json() : array();
return $this->response = $this->createResponse($jsonToArrayResponse, $httpResponse->getStatusCode());
} catch (\Exception $e) {
throw new InvalidResponseException(
'Error communicating with payment gateway: ' . $e->getMessage(),
Expand Down
6 changes: 4 additions & 2 deletions src/Message/RestTokenRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ function ($event) {
);

$httpResponse = $httpRequest->setAuth($this->getClientId(), $this->getSecret())->send();

return $this->response = new RestResponse($this, $httpResponse->json(), $httpResponse->getStatusCode());
// Empty response body should be parsed also as and empty array
$body = $httpResponse->getBody(true);
$jsonToArrayResponse = !empty($body) ? $httpResponse->json() : array();
return $this->response = new RestResponse($this, $jsonToArrayResponse, $httpResponse->getStatusCode());
}
}

0 comments on commit ec7fc86

Please sign in to comment.