Laravel HTTP x-www-form-urlencoded Request Body

Sometimes we need to send x-www-form-urlencoded form data. Most of the time, we get these types of errors:

  1. unsupported_grant_type
  2. 'grant_type' must be set

If you would like to send data using the application/x-www-form-urlencoded content type, you should call the asForm method before making your request:

$response = Http::withBasicAuth($client, $client_secret)
    ->asForm()
    ->post($endpoint, [
        'grant_type' => 'authorization_code',
        'code' => $request->code,
        'redirect_uri' => $redirect_url
    ])
    ->json();

Software Engineer | Ethical Hacker & Cybersecurity...

Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.