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

ansori24's avatar

400 Error when using Http::post

Hi, when I use Illuminate\Support\Facades\Http to make a post request to stable horde API

$response = Http::baseUrl(config('services.stablehorde.api_url'))
    ->withHeaders([
        'accept'       => 'application/json',
        'Content-Type' => 'application/x-www-form-urlencoded',
    ])
    ->post('/register', [
        'username' => $name,
    ]);
    
$content = $response->body();

I get errors like this :

<!doctype html>\n
<html lang=en>\n
<title>400 Bad Request</title>\n
<h1>Bad Request</h1>\n
<p>The browser (or proxy) sent a request that this server could not understand.</p>\n

but when I'm using curl, its works fine :

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, config('services.stablehorde.api_url') . '/register');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=$name");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$content = curl_exec($ch);
<!DOCTYPE html>
<html>
  <body>
    <div class="container">
      <div class="container">
        <h1>Welcome</h1>
        <h2>Set Display Name</h2>
	    .....

I don't want to use curl since I cannot test/mock the response. but I have no idea why the Laravel HTTP client throw and error. does anyone know why?

0 likes
5 replies
LaryAI's avatar
Level 58

It looks like the issue is with the headers you are sending with the request. The Content-Type header should be set to application/json instead of application/x-www-form-urlencoded. Try changing the headers to the following and see if that resolves the issue:

$response = Http::baseUrl(config('services.stablehorde.api_url'))
    ->withHeaders([
        'accept'       => 'application/json',
        'Content-Type' => 'application/json',
    ])
    ->post('/register', [
        'username' => $name,
    ]);
    
$content = $response->body();
ansori24's avatar

@LaryAI Nope, it didn't work, still got the same errors.

$response = Http::baseUrl(config('services.stablehorde.api_url'))
    ->withHeaders([
        'accept'       => 'application/json',
        'Content-Type' => 'application/json',
    ])
    ->post('/register', [
        'username' => $name,
    ]);

$content = $response->body();
<!doctype html>\n
<html lang=en>\n
<title>400 Bad Request</title>\n
<h1>Bad Request</h1>\n
<p>The browser (or proxy) sent a request that this server could not understand.</p>\n
neilstee's avatar

@ansori24 why do you need headers in the first place? Have you tried removing the headers entirely?

ansori24's avatar

@neilstee Yup, didn't work either. the response is still the same

<!doctype html>\n
<html lang=en>\n
<title>400 Bad Request</title>\n
<h1>Bad Request</h1>\n
<p>The browser (or proxy) sent a request that this server could not understand.</p>\n
neilstee's avatar

@ansori24 have you tried making a direct call instead of setting baseUrl?

$response = Http::post('https://urlhere.com/register', [
        'username' => $name,
    ]);

Please or to participate in this conversation.