Can you include raw JSON in Guzzle POST Body?
If your Curl command works perfectly but swapping it into a Guzzle client results in an "invalid request" error, the issue almost certainly isn't that Guzzle forbids raw JSON strings. The library accepts string payloads just fine; the problem lies in how the data is being parsed and how the framework wraps it before sending.
In the scenario described, the user attempts to pass a raw JSON string directly into the body option of the request:
$GetOrder = '{
"Filter": {
"OrderID": "N10139",
"OutputSelector": [
"OrderStatus"
]
}
}';
$res = $client->post(env('NETO_API_URL'), ['headers' => $headers], ['body' => $GetOrder]);
While this syntax is valid for a generic HTTP client, the context reveals a subtle but critical detail: the code snippet creates a new GuzzleHttp\Client instance without specifying an adapter. In many Laravel environments (especially older versions or specific package configurations), relying on the default Guzzle implementation can lead to unexpected behavior when dealing with raw strings versus arrays.
However, the most common culprit for "invalid request" errors in this specific setup is not the client configuration itself, but rather how the body parameter is interpreted by the underlying transport layer or how the headers interact with the payload encoding. When you pass a string directly to the body option, Guzzle treats it as raw bytes unless you explicitly configure the client to handle strings differently or if your server expects a specific content length that doesn't match the actual byte count of the string.
The String vs. Array Distinction
The primary distinction developers must respect is between passing an associative array and passing a JSON string.
If you pass a PHP array, Guzzle automatically serializes it to JSON and sets the Content-Type header to application/json. This is convenient but removes your control over specific formatting or escaping required by legacy APIs.
When passing a raw string (like $GetOrder above), you are responsible for ensuring the string is exactly what the server expects. The issue often arises if the string contains newlines or whitespace that differ between the Curl shell session and the PHP environment, although the most likely cause in a Laravel context involving client() is the adapter configuration.
The Adapter Misconfiguration
If the code explicitly instantiates \GuzzleHttp\Client, it should work with raw strings provided the headers are correct. However, if the environment defaults to a different client implementation (such as \Laravel\Community\Http\Client or similar wrappers used in specific packages), passing a raw string might trigger internal serialization logic that corrupts the payload.
The solution is to ensure you are using the standard Guzzle client and explicitly handling the body type. You can force Guzzle to treat the body as raw by ensuring the headers['Content-Type'] is set exactly as intended, which the provided code does correctly (application/json).
If the error persists, verify that the string variable $GetOrder does not contain any hidden characters or encoding issues. In PHP, it is often safer to use json_encode() to ensure strict formatting, even if you want to simulate raw input:
$payload = [
'Filter' => [
'OrderID' => 'N10139',
'OutputSelector' => ['OrderStatus']
]
];
// Guzzle handles the serialization and header setting automatically here
$res = $client->post(env('NETO_API_URL'), [
'headers' => $headers,
'body' => $payload
]);
By switching to an array structure, you bypass potential string encoding mismatches. If the API strictly requires a raw JSON string (for versioning or formatting reasons), ensure that the client is configured with curl as the handler explicitly:
$client = new \GuzzleHttp\Client([
'curl' => [CURLOPT_RETURNTRANSFER => true]
]);
$res = $client->post(env('NETO_API_URL'), [
'headers' => $headers,
'body' => $GetOrder
]);
Final Considerations
The "invalid request" error is typically a symptom of a mismatch between the payload format and what the server expects, rather than a limitation of Guzzle. If your Curl command uses curl -X POST -H "Content-Type: application/json" -d "$json_string", ensure that the PHP string matches that exact byte-for-byte representation.
For developers working with specific third-party APIs like Neto (referenced in the source material), always verify whether the API documentation specifies if it accepts raw JSON strings or if it expects a serialized object. In many cases, providing an array is the robust solution because it prevents escaping errors and ensures the Content-Type header is set correctly without manual intervention.
If you continue to face issues with specific legacy APIs that demand raw strings, consider using the body option with a stream resource or ensuring that no intermediate framework layers are wrapping the payload before it reaches Guzzle.