Laravel HTTP x-www-form-urlencoded Request Body

avatar
Published: Dec 08, 2022

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();