How to get response of content type application/x-www-form-urlencoded by passing parameters in laravel

痴心易碎 提交于 2020-12-15 08:32:17

问题


I have used Api on to Single Sign In Opt with in Laravel https://sso/{custom_path}/token like this Api created using jwt. And on my end in web application passing access token and content type in header to Api call using http client guzzle. With content type application/x-www-form-urlencoded with parameters in form_params. But in response i am getting missing grant_type. As i am passing grant_type in form_parms array. Is there any other way to resolve this issue. Any valueable response will be considered.

Code:

$uri = $this->userTokenAuthencticateUrl();
        $token = session('token')->access_token;
        $params['header'] = [
            "Content-Type: application/x-www-form-urlencoded",
            "Authorization: Bearer $token"
            ];
        $params['form_params'] = array(
                'grant_type' => 'xxxxx',
                'response_include_resource_name' => 'xxx',
                'audience' => 'xxxx', 
                'permission' => 'xxxxxx',
            );
            $response = Http::post($uri, $params);
            dd($response->json());

Ressponse:

array:2 [▼
  "error" => "invalid_request"
  "error_description" => "Missing form parameter: grant_type"
]

回答1:


As you are using HTTP Client. You need to change your code. You do not need to pass Content-Type as application/x-www-form-urlencoded in your header and I believe the Authorization token is passed separately in headers you can pas it in your params.

$uri = $this->userTokenAuthencticateUrl();
$token = session('token')->access_token;

 $params = array(
        'grant_type' => 'xxxxx',
        'response_include_resource_name' => 'xxx',
        'audience' => 'xxxx', 
        'permission' => 'xxxxxx',
 );
 $response = Http::asForm()->withHeaders([
        'Authorization' => 'Bearer ' . $token
     ])->post($uri, $params);

 dd($response->json());

Method 2:

It is also mentioned in docs

If you would like to quickly add an Authorization bearer token header to the request, you may use the withToken method so you can do like this as well

$uri = $this->userTokenAuthencticateUrl();
$token = session('token')->access_token;

 $params = array(
        'grant_type' => 'xxxxx',
        'response_include_resource_name' => 'xxx',
        'audience' => 'xxxx', 
        'permission' => 'xxxxxx',
 );
 $response = Http::asForm()->withToken($token)->post($uri, $params);

 dd($response->json());

See the doc for more details


Method 3:

You can even directly use guzzle as well.
define("form_params", \GuzzleHttp\RequestOptions::FORM_PARAMS );
try{
  $client = new \GuzzleHttp\Client(['headers' => ['Authorization' => 'Bearer ' . $token]]);
  
  $guzzleResponse = $client->post(
                $api_url, [
                'form_params' => [
                    'grant_type' => 'xxxxx',
                    'response_include_resource_name' => 'xxx',
                    'audience' => 'xxxx', 
                    'permission' => 'xxxxxx'
                ]
            ]);
    if ($guzzleResponse->getStatusCode() == 200) {
         $response = json_decode($guzzleResponse->getBody(),true);
         //perform your action with $response 
    } 
}
catch(\GuzzleHttp\Exception\RequestException $e){
   // you can catch here 400 response errors and 500 response errors
   // see this https://stackoverflow.com/questions/25040436/guzzle-handle-400-bad-request/25040600
}catch(Exception $e){
   //other errors 
}


来源:https://stackoverflow.com/questions/64227695/how-to-get-response-of-content-type-application-x-www-form-urlencoded-by-passing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!