Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stef25's avatar

HTTP client configuration

The following call returns an error from the APi I'm calling, which is a POST call with no data.

$response = Http::withToken('xxx')->post('https://domain.com/api/account/1/get_stuff', []);
dd($response->body());

However the following CURL call as generated by Postman works fine.

curl --location --request POST 'https://domain.com/api/account/1/get_stuff' --header 'Authorization: Bearer xxx'

The problem, I think, lies with the fact that no data should be sent but a post() call in Laravel's HTTP client does expect a second parameter with the POST data. Whether I pass NULL or an empty array I always get an error back from the API.

How can I copy this curl call and get it to not send any data at all with the POST request?

Making the request with Guzzle works fine but I prefer the brevity of Laravel's HTTP client.

0 likes
5 replies
rodrigo.pedra's avatar
Level 56

Try this:

$response = Http::withToken('xxx')
    ->bodyFormat('none') // can be anything different of json, form_params or multipart
    ->withOptions(['body' => null]) // set guzzle options
    ->post('https://domain.com/api/account/1/get_stuff', []);
rodrigo.pedra's avatar

I was thinking about it and maybe you don't need the withOptions(...), only setting bodyFormat(...) to something Guzzle doesn't expect as a body format shoudl suffice.

I tested locally and it should not send the body, but please test against your endpoint to see if it works:

$response = Http::withToken('xxx')
    ->bodyFormat('none')
    ->post('https://domain.com/api/account/1/get_stuff', []);
stef25's avatar

Hey, thanks so much for this.

It works with both bodyformatand withOptions, ie. your first reply. Both of these are required for the API I'm working with (Bandcamp).

Where did you find the bodyformat call, it's nowhere in the docs?

1 like
rodrigo.pedra's avatar

I looked into the Client source code.

When you call asForm internally it calls that method.

rodrigo.pedra's avatar

And I forgot to say on my last response: thanks for the update =)

Please or to participate in this conversation.