Skip to content

Commit a96a010

Browse files
authored
Merge pull request #15 from utopia-php/ser-728
Support JSON string body in fetch
2 parents c8cd9a5 + 33d461a commit a96a010

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/Client.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ private function withRetries(callable $callback): mixed
255255
*
256256
* @param string $url
257257
* @param string $method
258-
* @param array<string>|array<string, mixed> $body
258+
* @param array<string>|array<string, mixed>|string|null $body
259259
* @param array<string, mixed> $query
260260
* @param ?callable $chunks Optional callback function that receives a Chunk object
261261
* @param ?int $timeoutMs Optional request timeout in milliseconds
@@ -265,7 +265,7 @@ private function withRetries(callable $callback): mixed
265265
public function fetch(
266266
string $url,
267267
string $method = self::METHOD_GET,
268-
?array $body = [],
268+
array|string|null $body = [],
269269
?array $query = [],
270270
?callable $chunks = null,
271271
?int $timeoutMs = null,
@@ -275,7 +275,7 @@ public function fetch(
275275
throw new Exception("Unsupported HTTP method");
276276
}
277277

278-
if (isset($this->headers['content-type']) && $body !== null) {
278+
if (is_array($body) && isset($this->headers['content-type'])) {
279279
$body = match ($this->headers['content-type']) {
280280
self::CONTENT_TYPE_APPLICATION_JSON => $this->jsonEncode($body),
281281
self::CONTENT_TYPE_APPLICATION_FORM_URLENCODED, self::CONTENT_TYPE_MULTIPART_FORM_DATA => self::flatten($body),

tests/ClientTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ final class ClientTest extends TestCase
1313
* @dataProvider dataSet
1414
* @param string $url
1515
* @param string $method
16-
* @param array<string, mixed> $body
16+
* @param array<string, mixed>|string|null $body
1717
* @param array<string, string> $headers
1818
* @param array<string, mixed> $query
1919
* @return void
@@ -53,7 +53,7 @@ public function testFetch(
5353
if ($headers['content-type'] != "application/x-www-form-urlencoded") {
5454
$this->assertSame( // Assert that the body is equal to the response's body
5555
$respData['body'],
56-
json_encode($body) // Converting the body to JSON string
56+
is_string($body) ? $body : json_encode($body) // Converting the body to JSON string
5757
);
5858
}
5959
}
@@ -307,6 +307,25 @@ public function dataSet(): array
307307
'content-type' => 'application/json'
308308
],
309309
],
310+
'postSingleLineJsonStringBody' => [
311+
'localhost:8000',
312+
Client::METHOD_POST,
313+
'{"name": "John Doe","age": 30}',
314+
[
315+
'content-type' => 'application/json'
316+
]
317+
],
318+
'postMultiLineJsonStringBody' => [
319+
'localhost:8000',
320+
Client::METHOD_POST,
321+
'{
322+
"name": "John Doe",
323+
"age": 30
324+
}',
325+
[
326+
'content-type' => 'application/json'
327+
]
328+
],
310329
'postFormDataBody' => [
311330
'localhost:8000',
312331
Client::METHOD_POST,

0 commit comments

Comments
 (0)