Send a JSON POST request with Guzzle in PHP
In PHP, Guzzle can send a POST request carrying JSON by accepting your data as an array of key-value pairs through its json option.
This page explains that pattern, shows what a successful echo response looks like, and gives a short checklist for checking the request before you rely on it.
Use the json option
Create a Guzzle client, call its post method with the target URL, and place your payload in the json option as a PHP array of key-value pairs. You do not need to manually encode the array or add the JSON content type yourself when using this option. The protected example module code0001 shows the complete client setup, the POST call, and printing the response body.
What to look for in the response
The example posts two sample fields, key1 and key2, to an echo endpoint. In the printed response, those values appear both as the raw data string and as parsed entries under the json key. The response headers also show Content-Type: application/json, which is the quickest sign that the payload was sent as JSON rather than form-encoded data.
A quick verification checklist
Before sending real traffic, run the same shape of request against an echo endpoint and compare three things: the parsed json section matches your array, the content type is application/json, and the URL in the response matches the endpoint you intended. If the values appear under form instead of json, the payload was not sent through the json option. If the content type is missing, check that the option name and array structure are correct.
Further reading and related topics
For the full request-option reference, including the json option, consult the official Guzzle documentation. Related Guzzle topics worth checking next include HTTP basic authentication, SSL certificate errors, cURL usage, exception handling, client errors, proxies with authentication, and what Guzzle is used for in PHP.
| Check | What good looks like | If it differs |
|---|---|---|
| Parsed payload | Your keys appear under the json section of the response | Payload may have been sent as form data instead of JSON |
| Content type | Content-Type is application/json | Check the json option name and array structure |
| Target URL | Response URL matches the endpoint you posted to | Verify the URL passed to the post call |
Based on the echo-endpoint example in protected module code0001.
Frequently asked questions
How do I send JSON data with a Guzzle POST request?
Pass the payload as a PHP array under the json request option. Guzzle then sends it as JSON, with the Content-Type set to application/json, rather than as form fields.
What does the example response show?
The example sends key1 and key2 to an echo endpoint and prints the response body. The returned JSON shows the same two keys under json, confirming the payload arrived as parsed JSON.
Which Guzzle option should I use for JSON?
Use the json option for a JSON payload. That is the approach demonstrated in the example, where an array of key-value pairs is supplied through that option.