Laravel 8 - unsupported_grant_type

Viewed 608

I have a problem with the following:

  • I am creating an API (I'm new with this)
  • With this API I want to consume another API or WebService
  • In Postman I make a get request to the URL and it returns OK the access_token, but in the Laravel project it returns: "error" => "unsupported_grant_type".

This is my code:

use Illuminate\Support\Facades\Http;

$response = Http::withHeaders([
            'content-type' => 'application/x-www-form-urlencoded'
        ])->post('http://url:52904/Token', [
            'userName' => 'menganito',
            'password' => 'pepito',
            'grant_type' => 'password'
        ]);

        dd($response->json());

I tried installing ** Laravel Passport **, but I'm not using a DB for now.

1 Answers

The problem was x-www-form-urlencoded.

This solved my problem (asForm)

$response = Http::asForm()->post('http://url.com', [
                'userName' => 'user',
                'password' => 'pass',
                'grant_type' => 'password'
        ]);

dd($response->json());
Related