Skip to content

Commit

Permalink
Update withBody method to accept array as parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Thavarshan committed Oct 3, 2024
1 parent bf09f6a commit c05b84c
Show file tree
Hide file tree
Showing 11 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ FetchPHP’s fluent API provides the following methods for building requests:
$response = fetch()
->baseUri('https://example.com')
->withHeaders(['Content-Type' => 'application/json'])
->withBody(json_encode(['key' => 'value']))
->withBody(['key' => 'value'])
->withToken('fake-bearer-auth-token')
->post('/posts');

Expand All @@ -240,7 +240,7 @@ $data = null;
async(fn () => fetch()
->baseUri('https://example.com')
->withHeaders(['Content-Type' => 'application/json'])
->withBody(json_encode(['key' => 'value']))
->withBody(['key' => 'value'])
->withToken('fake-bearer-auth-token')
->post('/posts'))
->then(fn (ResponseInterface $response) => $data = $response->json()) // Success handler
Expand Down
4 changes: 2 additions & 2 deletions docs/api/client-handler.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ Sets the headers for the request.
### **`withBody()`**

```php
public function withBody(mixed $body): self
public function withBody(array $body): self
```

Sets the request body.

- **`$body`**: The content of the body, which could be a string, array (for JSON), or other data.
- **`$body`**: The content of the body, which should be an array as key-value pairs and will be encoded as JSON.

**Returns**: The `ClientHandler` instance for chaining.

Expand Down
2 changes: 1 addition & 1 deletion docs/api/fetch.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ echo $data['key'];
$response = fetch()
->baseUri('https://example.com')
->withHeaders('Content-Type', 'application/json')
->withBody(json_encode(['key' => 'value']))
->withBody(['key' => 'value'])
->withToken('fake-bearer-auth-token')
->post('/posts');

Expand Down
4 changes: 2 additions & 2 deletions docs/guide/async-requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ $data = null;
async(fn () => fetch()
->baseUri('https://example.com')
->withHeaders('Content-Type', 'application/json')
->withBody(json_encode(['key' => 'value']))
->withBody(['key' => 'value'])
->withToken('fake-bearer-auth-token')
->post('/posts'))
->then(fn (ResponseInterface $response) => $data = $response->json()) // Success handler
Expand Down Expand Up @@ -133,7 +133,7 @@ $data = null;
async(fn () => fetch()
->baseUri('https://example.com')
->withHeaders('Content-Type', 'application/json')
->withBody(json_encode(['key' => 'value']))
->withBody(['key' => 'value'])
->retry(3, 1000) // Retry 3 times with a 1-second delay between retries
->post('/posts'))
->then(fn (ResponseInterface $response) => $data = $response->json()) // Success handler
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/error-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ $error = null;
async(fn () => fetch()
->baseUri('https://example.com')
->withHeaders('Content-Type', 'application/json')
->withBody(json_encode(['key' => 'value']))
->withBody(['key' => 'value'])
->retry(3, 1000) // Retry 3 times with a 1-second delay between retries
->post('/posts'))
->then(fn (ResponseInterface $response) => $data = $response->json()) // Success handler
Expand Down
4 changes: 2 additions & 2 deletions docs/guide/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ FetchPHP’s fluent API allows you to chain methods to build and send HTTP reque
$response = fetch()
->baseUri('https://example.com')
->withHeaders('Content-Type', 'application/json')
->withBody(json_encode(['key' => 'value']))
->withBody(['key' => 'value'])
->withToken('fake-bearer-auth-token')
->post('/posts');

Expand All @@ -93,7 +93,7 @@ $data = null;
async(fn () => fetch()
->baseUri('https://example.com')
->withHeaders('Content-Type', 'application/json')
->withBody(json_encode(['key' => 'value']))
->withBody(['key' => 'value'])
->withToken('fake-bearer-auth-token')
->post('/posts'))
->then(fn (ResponseInterface $response) => $data = $response->json()) // Success handler
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/sync-requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ The Fluent API in FetchPHP allows for more flexible request building by chaining
$response = fetch()
->baseUri('https://example.com')
->withHeaders('Content-Type', 'application/json')
->withBody(json_encode(['key' => 'value']))
->withBody(['key' => 'value'])
->withToken('fake-bearer-auth-token')
->post('/posts');

Expand Down
6 changes: 3 additions & 3 deletions src/Fetch/Http/ClientHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,13 +349,13 @@ public function withHeaders(array $headers): self
/**
* Set the body for the request.
*
* @param mixed $body
* @param array $body
*
* @return self
*/
public function withBody(mixed $body): self
public function withBody(array $body): self
{
$this->options['body'] = $body;
$this->options['body'] = json_encode($body);

return $this;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Fetch/Interfaces/ClientHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public function withHeaders(array $headers): self;
/**
* Set the body for the request.
*
* @param mixed $body
* @param array $body
*
* @return self
*/
public function withBody(mixed $body): self;
public function withBody(array $body): self;

/**
* Set the query parameters for the request.
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/ClientHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
$response = $clientHandler->setSyncClient($mockClient)
->baseUri('http://localhost')
->withHeaders(['Content-Type' => 'application/json'])
->withBody(json_encode(['key' => 'value']))
->withBody(['key' => 'value'])
->withToken('fake-bearer-auth-token')
->post('/posts');

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/FetchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
->setSyncClient($mockClient) // Set the mock client
->baseUri('http://localhost')
->withHeaders(['Content-Type' => 'application/json'])
->withBody(json_encode(['key' => 'value']))
->withBody(['key' => 'value'])
->withToken('fake-bearer-auth-token')
->post('/posts');

Expand Down

0 comments on commit c05b84c

Please sign in to comment.