简体   繁体   English

如何通过在laravel中传递参数来获得内容类型application/x-www-form-urlencoded的响应

[英]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());

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如果您想快速向请求添加授权不记名令牌标头,您可以使用 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有关更多详细信息,请参阅文档


Method 3:方法三:

You can even directly use guzzle as well. 您甚至可以直接使用 guzzle。
 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.

相关问题 file_get_contents():假设应用程序/x-www-form-urlencoded 未指定内容类型 - file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded 如何使用内容类型从 webhook 获取数据:application/x-www-form-urlencoded;charset=UTF-8? - How to get data from webhook with content-type: application/x-www-form-urlencoded;charset=UTF-8? file_get_contents():假设application / x-www-form-urlencoded with imgur API,未指定Content-type - file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded with imgur API 重新验证错误; 注意:file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded in - recaptcha error; Notice: file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded in 使用Content-Type = application / x-www-form-urlencoded发送POST请求时,POST值为空 - POST value is empty when Sending POST request with Content-Type = application/x-www-form-urlencoded PHP - 假设应用程序/x-www-form-urlencoded 未指定内容类型 - PHP - Content-type not specified assuming application/x-www-form-urlencoded 如何使用内容类型为“ application / x-www-form-urlencoded”的PHP curl发送原始JSON? - How can I send a raw JSON using PHP curl with the content type “application/x-www-form-urlencoded”? 使用标题应用程序/ x-www-form-urlencoded进行卷曲发布 - curl posting with header application/x-www-form-urlencoded ionic和php之间的application / x-www-form-urlencoded - application/x-www-form-urlencoded between ionic and php 的file_get_contents( 'PHP://输入'); 与application / x-www-form-urlencoded; - file_get_contents('php://input'); with application/x-www-form-urlencoded;
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM