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

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

问题描述

我已经在 Laravel https://sso/{custom_path}/token 中使用 Api 进行单点登录选项,就像使用 jwt 创建的这个 Api.在我的 Web 应用程序中,使用 http 客户端 guzzle 将标头中的访问令牌和内容类型传递给 Api 调用.内容类型为 application/x-www-form-urlencoded,参数为 form_params.但作为回应,我丢失了grant_type.当我在 form_parms 数组中传递 grant_type 时.有没有其他方法可以解决这个问题.任何有价值的回应都会被考虑.

代码:

$uri = $this->userTokenAuthencticateUrl();$token = session('token')->access_token;$params['header'] = [内容类型:应用程序/x-www-form-urlencoded",授权:Bearer $token"];$params['form_params'] = 数组('grant_type' =>'xxxxxx','response_include_resource_name' =>'xxx','观众' =>'xxx','权限' =>'xxxxxx',);$response = Http::post($uri, $params);dd($response->json());

回复:

array:2 [▼错误"=>无效请求"错误描述"=>缺少表单参数:grant_type"]

解决方案

当您使用 HTTP 客户端时.你需要改变你的代码.您不需要在标题中将 Content-Type 作为 application/x-www-form-urlencoded 传递,我相信授权令牌是在标题中单独传递的,您可以将其传递到参数中.>

$uri = $this->userTokenAuthencticateUrl();$token = session('token')->access_token;$params = 数组('grant_type' =>'xxxxxx','response_include_resource_name' =>'xxx','观众' =>'xxx','权限' =>'xxxxxx',);$response = Http::asForm()->withHeaders(['授权' =>'承载'.$令牌])->post($uri, $params);dd($response->json());

方法 2:

它也在文档中提到<块引用>

如果你想快速给请求添加一个 Authorization 不记名令牌头,你可以使用 withToken 方法所以你也可以这样做

$uri = $this->userTokenAuthencticateUrl();$token = session('token')->access_token;$params = 数组('grant_type' =>'xxxxxx','response_include_resource_name' =>'xxx','观众' =>'xxx','权限' =>'xxxxxx',);$response = Http::asForm()->withToken($token)->post($uri, $params);dd($response->json());

查看文档了解更多详情


方法 3:

您甚至可以直接使用 guzzle.

define(form_params", \GuzzleHttp\RequestOptions::FORM_PARAMS );尝试{$client = new \GuzzleHttp\Client(['headers' => ['Authorization' => 'Bearer' . $token]]);$guzzleResponse = $client->post($api_url, ['form_params' =>['grant_type' =>'xxxxxx','response_include_resource_name' =>'xxx','观众' =>'xxx','权限' =>'xxxxxx']]);如果 ($guzzleResponse->getStatusCode() == 200) {$response = json_decode($guzzleResponse->getBody(),true);//使用 $response 执行您的操作}}catch(\GuzzleHttp\Exception\RequestException $e){//您可以在这里捕获 400 个响应错误和 500 个响应错误//看到这个 https://stackoverflow.com/questions/25040436/guzzle-handle-400-bad-request/25040600}catch(异常$e){//其他错误}

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"
]

解决方案

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 
}

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

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