Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ private function withRetries(callable $callback): mixed
*
* @param string $url
* @param string $method
* @param array<string>|array<string, mixed> $body
* @param array<string>|array<string, mixed>|string|null $body
* @param array<string, mixed> $query
* @param ?callable $chunks Optional callback function that receives a Chunk object
* @param ?int $timeoutMs Optional request timeout in milliseconds
Expand All @@ -265,7 +265,7 @@ private function withRetries(callable $callback): mixed
public function fetch(
string $url,
string $method = self::METHOD_GET,
?array $body = [],
array|string|null $body = [],
?array $query = [],
?callable $chunks = null,
?int $timeoutMs = null,
Expand All @@ -275,7 +275,7 @@ public function fetch(
throw new Exception("Unsupported HTTP method");
}

if (isset($this->headers['content-type']) && $body !== null) {
if (is_array($body) && isset($this->headers['content-type'])) {
$body = match ($this->headers['content-type']) {
self::CONTENT_TYPE_APPLICATION_JSON => $this->jsonEncode($body),
self::CONTENT_TYPE_APPLICATION_FORM_URLENCODED, self::CONTENT_TYPE_MULTIPART_FORM_DATA => self::flatten($body),
Expand Down
23 changes: 21 additions & 2 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class ClientTest extends TestCase
* @dataProvider dataSet
* @param string $url
* @param string $method
* @param array<string, mixed> $body
* @param array<string, mixed>|string|null $body
* @param array<string, string> $headers
* @param array<string, mixed> $query
* @return void
Expand Down Expand Up @@ -53,7 +53,7 @@ public function testFetch(
if ($headers['content-type'] != "application/x-www-form-urlencoded") {
$this->assertSame( // Assert that the body is equal to the response's body
$respData['body'],
json_encode($body) // Converting the body to JSON string
is_string($body) ? $body : json_encode($body) // Converting the body to JSON string
);
}
}
Expand Down Expand Up @@ -307,6 +307,25 @@ public function dataSet(): array
'content-type' => 'application/json'
],
],
'postSingleLineJsonStringBody' => [
'localhost:8000',
Client::METHOD_POST,
'{"name": "John Doe","age": 30}',
[
'content-type' => 'application/json'
]
],
'postMultiLineJsonStringBody' => [
'localhost:8000',
Client::METHOD_POST,
'{
"name": "John Doe",
"age": 30
}',
[
'content-type' => 'application/json'
]
],
'postFormDataBody' => [
'localhost:8000',
Client::METHOD_POST,
Expand Down