Skip to content

Commit

Permalink
content-type only when set when json is data and pdf files, content-l…
Browse files Browse the repository at this point in the history
…engt only when there is data.
  • Loading branch information
steffjenl committed Jan 10, 2020
1 parent 62d4118 commit 1dc0b6f
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/SignhostClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public function __construct(
array $requestOptions = []
) {
$this->headers = [
'Content-Type: application/json',
"Application: APPKey $appName $appKey",
"Authorization: APIKey $apiKey",
];
Expand Down Expand Up @@ -81,19 +80,19 @@ public function performRequest(string $endpoint, string $method, $data = null, $
if (isset($filePath)) {
$uploadFileHandle = fopen($filePath, 'rb');

$headers[0] = 'Content-Type: application/pdf';
$headers[] = 'Content-Type: application/pdf';
$headers[] = 'Digest: SHA256=' . base64_encode(pack('H*', hash_file('sha256', $filePath)));
}

// If method is a GET or HEAD request, unset the Content-Type headers if filePath is not given
// GET/HEAD methods never have a content Type
if ("GET" === $method || "HEAD" === $method && !isset($filePath)) {
unset($headers[0]); // unset Content-Type
// When data is set, we must add Content-Type: application/json header
if (isset($data) && !empty($data) && $this->isValidJson($data)) {
$headers[] = 'Content-Type: application/json';
$headers[] = 'Content-Length: ' . strlen($data);
}

// for start transaction, SignHost will require the content-lenth: 0 header.
if (false !== strpos($endpoint, 'start')) {
$headers[] = "Content-Length: 0";
$headers[] = 'Content-Length: 0';
}

// Initialize a cURL session
Expand All @@ -114,6 +113,11 @@ public function performRequest(string $endpoint, string $method, $data = null, $
}
}

private function isValidJson($jsonString) {
json_decode($jsonString);
return (json_last_error() == JSON_ERROR_NONE);
}

/**
* @throws SignhostException
*/
Expand Down

0 comments on commit 1dc0b6f

Please sign in to comment.