From 1d7e19dbf2879b0e890c5e8bc12e4d1c15568074 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Tue, 9 May 2023 08:01:41 -0400 Subject: [PATCH 1/6] add the option to set the full payload as json-encoded value --- src/Network/Http.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Network/Http.php b/src/Network/Http.php index fc9c4943e..7ab44ff89 100644 --- a/src/Network/Http.php +++ b/src/Network/Http.php @@ -1,6 +1,7 @@ $_value) { $this->data($_key, $_value); } - return $this; + } elseif (is_null($value) && Str::isJson($key)) { + $this->requestData = $key; + } else { + $this->requestData[$key] = $value; } - - $this->requestData[$key] = $value; return $this; } From bbdd89025f616843caf508ab3dafaaad6c608059 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Tue, 9 May 2023 08:53:45 -0400 Subject: [PATCH 2/6] add json helper method --- src/Network/Http.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Network/Http.php b/src/Network/Http.php index 7ab44ff89..8925a2c5f 100644 --- a/src/Network/Http.php +++ b/src/Network/Http.php @@ -115,7 +115,7 @@ class Http public $requestOptions = []; /** - * @var array Request data. + * @var array|string Request data. */ public $requestData; @@ -442,6 +442,20 @@ protected function headerToArray($header) return $headers; } + /** + * Add JSON encoded payload + */ + public function json(array|string $payload) : self + { + if (is_array($payload)) { + $payload = json_encode($payload); + } + $this->requestData = $payload; + $this->header('Content-Type', 'application/json'); + + return $this; + } + /** * Add a data to the request. * @param string $value From e8cc1e026c97bb885d454c23e1755fcb06abad67 Mon Sep 17 00:00:00 2001 From: Ben Thomson Date: Tue, 9 May 2023 21:14:47 +0800 Subject: [PATCH 3/6] Update src/Network/Http.php --- src/Network/Http.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Network/Http.php b/src/Network/Http.php index 8925a2c5f..adbc63d25 100644 --- a/src/Network/Http.php +++ b/src/Network/Http.php @@ -445,7 +445,7 @@ protected function headerToArray($header) /** * Add JSON encoded payload */ - public function json(array|string $payload) : self + public function json(array|string $payload): self { if (is_array($payload)) { $payload = json_encode($payload); From 49727ce3292606e0d70eeb6188d813e7024b80ac Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Tue, 9 May 2023 09:24:18 -0400 Subject: [PATCH 4/6] add proper type hints for method parameters --- src/Network/Http.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Network/Http.php b/src/Network/Http.php index adbc63d25..7c6e436dd 100644 --- a/src/Network/Http.php +++ b/src/Network/Http.php @@ -458,17 +458,13 @@ public function json(array|string $payload): self /** * Add a data to the request. - * @param string $value - * @return self */ - public function data($key, $value = null) + public function data(array|string $key, string $value = null): self { if (is_array($key)) { foreach ($key as $_key => $_value) { $this->data($_key, $_value); } - } elseif (is_null($value) && Str::isJson($key)) { - $this->requestData = $key; } else { $this->requestData[$key] = $value; } From fd87bb4598ce5a6d848e0c627d474ba21dfd8515 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Tue, 1 Aug 2023 17:17:04 -0400 Subject: [PATCH 5/6] try to json_encode anything thrown at us --- src/Network/Http.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Network/Http.php b/src/Network/Http.php index 7c6e436dd..3c39543a0 100644 --- a/src/Network/Http.php +++ b/src/Network/Http.php @@ -445,11 +445,14 @@ protected function headerToArray($header) /** * Add JSON encoded payload */ - public function json(array|string $payload): self + public function json(mixed $payload): self { - if (is_array($payload)) { - $payload = json_encode($payload); + if (!Str::isJson($payload)) { + if (!$payload = json_encode($payload)) { + throw new ApplicationException('provided payload cannot be json_encoded '); + } } + $this->requestData = $payload; $this->header('Content-Type', 'application/json'); From 10a09a9a4b014e49a01e4b8a93c04afe3adebc6a Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Tue, 1 Aug 2023 19:36:37 -0600 Subject: [PATCH 6/6] Update src/Network/Http.php --- src/Network/Http.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Network/Http.php b/src/Network/Http.php index 3c39543a0..693365aa4 100644 --- a/src/Network/Http.php +++ b/src/Network/Http.php @@ -449,7 +449,7 @@ public function json(mixed $payload): self { if (!Str::isJson($payload)) { if (!$payload = json_encode($payload)) { - throw new ApplicationException('provided payload cannot be json_encoded '); + throw new ApplicationException('The provided payload failed to be encoded as JSON'); } }