From cf8b2864ad009f09b23e0bebf61e4149f8502f10 Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Thu, 22 Jun 2023 03:05:29 +0200 Subject: [PATCH] [10.x] Add `withQueryParameters` to the HTTP client (#47297) * Add `withQueryParameters` to the HTTP client Some APIs require some query parameters to be always set. For example auth token (even though this isn't a great practice). With this new helper, we can create a macro with the query parameter always set. * Add more tests for `withQueryParameters` with arrays See https://github.com/laravel/framework/pull/47297#issuecomment-1572587392 * formatting --------- Co-authored-by: Taylor Otwell --- Client/PendingRequest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Client/PendingRequest.php b/Client/PendingRequest.php index 9aa08cf5..d6689ccc 100644 --- a/Client/PendingRequest.php +++ b/Client/PendingRequest.php @@ -344,6 +344,21 @@ public function bodyFormat(string $format) }); } + /** + * Set the given query parameters in the request URI. + * + * @param array $parameters + * @return $this + */ + public function withQueryParameters(array $parameters) + { + return tap($this, function () use ($parameters) { + $this->options = array_merge_recursive($this->options, [ + 'query' => $parameters, + ]); + }); + } + /** * Specify the request's content type. *