Try specifying that you accept application/json using the acceptJson() convenience method:
$request = Http::acceptJson()
->withHeaders($this->headers())
->post($url, $fields);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm sending data to an API using this code:
$request = Http::withHeaders($this->headers())
->post($url, $fields);
$this->headers() returns an array with the API key, $fields is an array of fields to send to the API. When I send the request using an HTTP client like Postman or RESTClient I get a JSON response when the request fails; but when I send the request in Laravel, I only end with an empty response body. I tried fiddling with the headers, sendind Accept and Content-Type headers, no difference.
What am I missing? Why is the response body empty when it shouldn't?
OK, solved it. Don't ask me why, but using this makes it work:
Http::withHeaders($this->headers())
->withBody(json_encode($fields), 'application/json')
->post($url);
No idea why manually filling the request body with JSON instead of using Http::post($url, ['foo' => 'bar']) has an influence on the response body, but still, seems to work.
Please or to participate in this conversation.