json
- Summary
- The
json
option is used to easily upload JSON encoded data as thebody of a request. A Content-Type header ofapplication/json
will beadded if no Content-Type header is already present on the message. - Types
- Any PHP type that can be operated on by PHP's
json_encode()
function. - Default
- None
- Constant
GuzzleHttp\RequestOptions::JSON
- $response = $client->request('PUT', '/put', ['json' => ['foo' => 'bar']]);
Here's an example of using the tap
middleware to see what request is sentover the wire.
- use GuzzleHttp\Middleware;
- // Grab the client's handler instance.
- $clientHandler = $client->getConfig('handler');
- // Create a middleware that echoes parts of the request.
- $tapMiddleware = Middleware::tap(function ($request) {
- echo $request->getHeaderLine('Content-Type');
- // application/json
- echo $request->getBody();
- // {"foo":"bar"}
- });
- $response = $client->request('PUT', '/put', [
- 'json' => ['foo' => 'bar'],
- 'handler' => $tapMiddleware($clientHandler)
- ]);
Note
This request option does not support customizing the Content-Type headeror any of the options from PHP's json_encode()function. If you need to customize these settings, then you must pass theJSON encoded data into the request yourself using the body
requestoption and you must specify the correct Content-Type header using theheaders
request option.
This option cannot be used with body
, form_params
, or multipart