Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit 52ca693

Browse files
committed
Merge branch 'feature/87-json-request-bodies'
Close #90 Fixes #87
2 parents df9919e + 4904554 commit 52ca693

File tree

4 files changed

+337
-77
lines changed

4 files changed

+337
-77
lines changed

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@ Releases prior to 1.2.0 did not have entries.
1818

1919
### Changed
2020

21-
- Nothing.
21+
- [#90](https://github.com/zendframework/zenddiagnostics/pull/90) modifies what types are allowed for the `GuzzleHttpService` initial constructor
22+
argument. Previously, it only allowed a URL; it now allow any valid request instance the Guzzle client
23+
can accept. This change allows you to craft a custom request to send.
24+
25+
- [#90](https://github.com/zendframework/zenddiagnostics/pull/90) modifies the behavior of `GuzzleHttpService` slightly in relation to how
26+
it handles its `$body` argument. It now allows stream instances, any object implementing `__toString()`,
27+
any iterator objects, any `JsonSerializable` objects, and strings and arrays. In the latter case, it
28+
checks to see if the request `Content-Type` is JSON, casting the value to JSON if so, and otherwise
29+
serializing it as form-encoded data.
2230

2331
### Deprecated
2432

docs/book/diagnostics.md

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,37 @@ $checkPageContent = new HttpService(
192192
## GuzzleHttpService
193193

194194
Attempt connection to a given HTTP host or IP address and try to load a web page
195-
using [Guzzle](http://guzzle3.readthedocs.org/en/latest/). The check also
196-
supports checking response codes and page contents.
195+
using [Guzzle](http://docs.guzzlephp.org). The check also supports checking
196+
response codes and page contents.
197+
198+
The constructor signature of the `GuzzleHttpService` is as follows:
199+
200+
```php
201+
/**
202+
* @param string|Psr\Http\Message\RequestInterface|GuzzleHttp\Message\RequestInterface $requestOrUrl
203+
* The absolute url to check, or a fully-formed request instance.
204+
* @param array $headers An array of headers used to create the request
205+
* @param array $options An array of guzzle options to use when sending the request
206+
* @param int $statusCode The response status code to check
207+
* @param null $content The response content to check
208+
* @param null|GuzzleHttp\ClientInterface $guzzle Instance of guzzle to use
209+
* @param string $method The method of the request
210+
* @param mixed $body The body of the request (used for POST, PUT and DELETE requests)
211+
* @throws InvalidArgumentException
212+
*/
213+
public function __construct(
214+
$requestOrUrl,
215+
array $headers = [],
216+
array $options = [],
217+
$statusCode = 200,
218+
$content = null,
219+
$guzzle = null,
220+
$method = 'GET',
221+
$body = null
222+
)
223+
```
224+
225+
Examples:
197226

198227
```php
199228
<?php
@@ -229,6 +258,33 @@ $checkPageContent = new GuzzleHttpService(
229258
);
230259
```
231260

261+
You can send JSON data by either providing a `Content-Type` header that includes
262+
a JSON content type, or creating a request instance with JSON content:
263+
264+
```php
265+
// Send page content
266+
$checkPageContent = new GuzzleHttpService(
267+
'api.example.com/ping',
268+
['Content-Type' => 'application/json'],
269+
[],
270+
200,
271+
null,
272+
null,
273+
'POST',
274+
['ping' => microtime()]
275+
);
276+
277+
// Assuming Guzzle 6:
278+
use GuzzleHttp\Psr7\Request;
279+
$request = new Request(
280+
'POST',
281+
'http://api.example.com/ping',
282+
['Content-Type' => 'application/json'],
283+
json_encode(['ping' => microtime()])
284+
);
285+
$checkPageContent = new GuzzleHttpService($request);
286+
```
287+
232288
## Memcache
233289

234290
Attempt to connect to given Memcache server.

0 commit comments

Comments
 (0)