如何通过传递参数在laravel中获得内容类型application/x-www-form-urlencoded的响应 [英] How to get response of content type application/x-www-form-urlencoded by passing parameters in laravel

查看:91
本文介绍了如何通过传递参数在laravel中获得内容类型application/x-www-form-urlencoded的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Laravel https://sso/{custom_path}/token中将Api应用于Single Sign In Opt,就像使用jwt创建的Api一样.最后,在Web应用程序中,使用http客户端接口将访问令牌和内容类型的标头传递给Api调用.内容类型为application/x-www-form-urlencode,在form_params中带有参数.但是作为回应,我越来越缺少Grant_type.当我在form_parms数组中传递grant_type时.还有其他方法可以解决此问题.任何有价值的回应都将被考虑.

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.

代码:

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

响应:

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

推荐答案

使用HTTP客户端时.您需要更改代码.您不需要在标头中以 application/x-www-form-urlencoded 的形式传递Content-Type,并且我相信Authorization令牌是在标头中单独传递的,您可以将其放在参数中.

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

方法2:

在文档中也提到了

如果您想快速向请求添加授权承载令牌标头,则可以使用withToken方法所以你也可以这样做

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

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 
}

这篇关于如何通过传递参数在laravel中获得内容类型application/x-www-form-urlencoded的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆