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

prospero's avatar

HTTP Facade

Hi guys. Now I'm introducing in API requests and it's stuff and I'm very exciting. Doing this

$data = http_build_query([
            'grant_type' => 'authorization_code',
            'code' => $request->code,
            'redirect_uri' => 'http://...',
            'client_id' => '...',
            'client_secret' => '...',
]);
$opts = array('http' =>
       array(
           'method'  => 'POST',
           'header'  => 'Content-Type: application/x-www-form-urlencoded',
           'content' => $data
            )
       );
$context  = stream_context_create($opts);
$result = file_get_contents('https://api.....', false, $context);

This work, I see there is some other ways to do it more easy and more efficient. But I'm looking through the Http Facade and the Laravel documentation for achieve the same result with before code. I try this but isn't right

$response = Http::withHeaders([
            'Content-Type' => 'application/x-www-form-urlencoded'
        ])->withOptions([
            'grant_type' => 'authorization_code',
            'code' => $request->code,
            'redirect_uri' => 'http://...',
            'client_id' => '....',
            'client_secret' => '....',
        ])->post('https://api.....');

some help with?

0 likes
14 replies
laracoft's avatar

@prospero

$response = Http::withHeaders([
        "Accept-Language" => "en_US",
        'Accept' => 'application/x-www-form-urlencoded',
    ])
    ->post('https://domain.com/api/token', [
        'code' => '...',
        'grant_type' => '...',
        'client_id' => '...',
        'client_secret' => '...',
        'redirect_uri' => '...'
    ]);
marosmjartan's avatar

Try this

$response = Http::withHeaders([
    'Content-Type' => 'application/x-www-form-urlencoded'
])->post('https://api.....', [
    'grant_type' => 'authorization_code',
    'code' => $request->code,
    'redirect_uri' => 'http://...',
    'client_id' => '....',
    'client_secret' => '....',
]);

prospero's avatar

Thanks both. As the same request provide by you, I'm getting this error

"error" => "invalid_request"
  "error_description" => "The grant_type property is required."
  "error_code" => 1

something wrong...did you recognize this?

laracoft's avatar

@prospero

$response = Http::withHeaders([
        'Accept' => 'application/x-www-form-urlencoded',
        'Accept-Language' => 'en_US',
    ])
    ->post('https://domain.com/api/token', [
        'form_params' => [
            'code' => '...',
            'grant_type' => '...',
            'client_id' => '...',
            'client_secret' => '...',
            'redirect_uri' => '...',
        ],
    ]);
prospero's avatar

@laracoft same error back :(

array:3 [▼
  "error" => "invalid_request"
  "error_description" => "The grant_type property is required."
  "error_code" => 1
]
laracoft's avatar

@prospero

Which API is this? Does it accept application/json?

$response = Http::withHeaders([
        "Accept-Language" => "en_US",
        'Accept' => 'application/json',
    ])
    ->post('https://domain.com/api/token', [
        'code' => '...',
        'grant_type' => '...',
        'client_id' => '...',
        'client_secret' => '...',
        'redirect_uri' => '...'
    ]);
prospero's avatar

The TeamViewer API. In documentation said this: POST /api/v1/oauth2/token (token endpoint) Parameters must be inside the body of the request and encoded with the "application/x-www-formurlencoded" format. This is the only exception where the body is not JSON or XML.

laracoft's avatar

@prospero

$response = Http::withHeaders([
        'Accept' => 'application/x-www-form-urlencoded',
        'Accept-Language' => 'en_US',
    ])->request('POST', 'https://domain.com/api/token', [
        'form_params' => [
            'code' => '...',
            'grant_type' => '...',
            'client_id' => '...',
            'client_secret' => '...',
            'redirect_uri' => '...',
        ],
    ]);
prospero's avatar

@laracoft No, don't work

BadMethodCallException
Method Illuminate\Http\Client\PendingRequest::request does not exist.
laracoft's avatar

@prospero

What is the version of your Laravel and guzzle?

Type composer show | grep guzzle on your CLI.

laracoft's avatar
laracoft
Best Answer
Level 27

@prospero

So sorry, more used to guzzle rather than Http:: This should sort it out.

$response = Http::asForm()->post('https://domain.com/api/token', [
    'code' => '...',
    'grant_type' => '...',
    'client_id' => '...',
    'client_secret' => '...',
    'redirect_uri' => '...',
]);
1 like
prospero's avatar

@laracoft this is a fresh test project

Laravel Framework 8.38.0
guzzlehttp/guzzle                  7.3.0   Guzzle is a PHP HTTP client library
guzzlehttp/promises                1.4.1   Guzzle promises library
guzzlehttp/psr7                    1.8.1   PSR-7 message implementation that also provides common utility methods
prospero's avatar

Now you hit it!!!! hahaha, it's working like a charm. Thanks buddy

Please or to participate in this conversation.