[英]How to get response of content type application/x-www-form-urlencoded by passing parameters in laravel
I have used Api on to Single Sign In Opt with in Laravel https://sso/{custom_path}/token like this Api created using jwt.我已经在 Laravel https://sso/{custom_path}/token 中使用 Api 进行单点登录选项,就像使用 jwt 创建的这个 Api。 And on my end in web application passing access token and content type in header to Api call using http client guzzle.
在我的 Web 应用程序中,使用 http 客户端 guzzle 将标头中的访问令牌和内容类型传递给 Api 调用。 With content type application/x-www-form-urlencoded with parameters in form_params.
内容类型为 application/x-www-form-urlencoded,参数为 form_params。 But in response i am getting missing grant_type.
但作为回应,我丢失了grant_type。 As i am passing grant_type in form_parms array.
当我在 form_parms 数组中传递 grant_type 时。 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.当您使用 HTTP 客户端时。 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.您不需要在标题中将 Content-Type 作为
application/x-www-form-urlencoded
传递,我相信授权令牌是在标题中单独传递的,您可以将其传递到参数中。
$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());
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
如果您想快速向请求添加授权不记名令牌标头,您可以使用 withToken 方法,这样您也可以这样做
$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 }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.