How can I use Guzzle to send a POST request in JSON?

How can I use Guzzle to send a POST request in JSON?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Hidden Trap in Guzzle POST Requests

The code snippet you are looking at illustrates a very common pitfall when interacting with APIs: manually setting headers while trying to handle body content as a string. The specific issue causing your Internal Server Error is the use of json_encode($_POST) combined with 'content-type' => 'application/json'. While this mimics what Postman does, it fundamentally misunderstands how Guzzle handles request bodies and how modern PHP frameworks parse incoming data.

When you pass a string to the body of a Guzzle request, Guzzle sends that exact string as the payload. By using json_encode($_POST), you are taking the raw values from your superglobal array and converting them into a JSON string. However, most robust API endpoints do not expect a raw JSON string in the body; they expect an associative array. When the server receives a raw JSON string, it attempts to decode it expecting a specific structure or uses a middleware that fails because the content type is handled differently than expected by the framework parsing the request.

The core difference between your code and Postman lies in how the data is prepared before transmission. In Postman, when you select "raw" text and choose JSON, you are essentially telling the tool to treat the input as a raw string that needs to be sent exactly as typed. If you type {"name": "John", "role": "admin"} into Postman's raw JSON tab, it sends those specific characters. However, if you use Postman's automatic body parsing features or simply enter data in the form fields, Postman automatically handles the serialization and sets the content-type header correctly. The manual approach in your script attempts to replicate the raw string behavior but often fails because $_POST contains superglobal keys that might not align with the API's expected schema, or the string encoding introduces subtle whitespace or formatting differences that the server rejects.

To fix this and align your Guzzle request with how modern frameworks expect data, you should leverage Guzzle's built-in support for associative arrays. Instead of converting $_POST into a JSON string manually, pass the data directly as an array to the body parameter. Guzzle is smart enough to detect that you are sending an array and will automatically serialize it to JSON and set the appropriate Content-Type: application/json header for you.

Here is the corrected implementation:

use GuzzleHttp\Client;

$client = new Client();

// Pass the data as an associative array, not a string
$response = $client->post(self::URL_REGISTER, [
    'json' => $_POST, 
]);

// Or alternatively using the body parameter with automatic serialization
// $response = $client->post(self::URL_REGISTER, [
//     'body' => json_encode($_POST), 
//     'headers' => ['Content-Type' => 'application/json']
// ]);

The first option shown above is significantly preferred. By using the 'json' parameter in Guzzle, you avoid manual string encoding entirely. The library handles the Content-Type header automatically, ensuring it matches the payload exactly. This approach ensures that if your server expects a specific JSON structure, Guzzle sends it in a format that standard PHP frameworks can decode immediately without needing to guess how the data was serialized.

If you must stick to the 'body' parameter, ensure that the key is capitalized consistently ('headers' vs 'content-type'). In Guzzle, keys are case-sensitive when defining headers explicitly within the array. Using lowercase 'content-type' might fail in certain versions or configurations where strict header normalization occurs, whereas uppercase 'Content-Type' is the standard HTTP header format expected by the underlying cURL layer.

This distinction between passing a raw string versus an associative array is one of the most frequent sources of Internal Server Error responses when migrating from manual curl scripts to Guzzle. By letting the library manage the serialization and header injection, you eliminate discrepancies in how the data looks on the wire, ensuring your requests match exactly what tools like Postman send by default.

Our network