Yii2 Rest身份验证承载发布数据丢失 [英] Yii2 Rest Authentication Bearer Post Data missing

查看:87
本文介绍了Yii2 Rest身份验证承载发布数据丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究REST API.因此,我准备了一个函数,该函数通过curl将身份验证数据发送到REST Server.我已经实现了两个身份验证选项.第一个是基本身份验证,第二个是通过令牌(承载)身份验证.

I'am working on a REST API. Therefore I prepared a function which sends the authentication data via curl to the REST Server. I've implemented two authentication options. The first is Basic Authentication, the second is authentication via token (Bearer).

现在,我很麻烦,因为在REST服务器上,如果通过令牌进行身份验证,则REST服务器不会接收POST数据.身份验证本身是有效的,但是POST数据将丢失.如果通过基本身份验证进行身份验证,则REST服务器将接收到POST数据,没问题.

Now, i'am in trobles, because at the REST Server, the POST Data is not received by the REST Server in case of authentication via token. The authentication itself is working, but the POST Data is going to be lost. In case of authentication via Basis Authentication, the POST Data will be received by the REST Server, no problem.

private function request($postdata){

    $url = $this->service_url_private;
    $curl = curl_init($url);
    $curl_post_data = $postdata;


    // check if token authentication is used
    if (array_key_exists('token', $postdata )){
        $token = $postdata['token'];
        $authorization = 'Authorization: Bearer ' . $token;
        // prepare curl for Bearer Token Authorization
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization));
    } else {
        // otherwise use BASIC authentication
        if (array_key_exists('email', $postdata )){
            $username = $postdata['email'];
        }
        if (array_key_exists('password', $postdata )){
            $password = $postdata['password'];
        }
        // prepare curl for Basic Authentication
        curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
    }

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
    curl_setopt($curl, CURLINFO_HEADER_OUT, true); // Detail information for debugging
    curl_setopt($curl, CURLOPT_VERBOSE,true);
    $curl_response = curl_exec($curl); // Detail information for debugging
    $info = curl_getinfo($curl); // Detail information for debugging
    curl_close($curl);
    var_dump($info);
    return $curl_response;
}

此外,$ curl_post_data在调试时由客户端显示所有数据,然后其余的调用将使用curl_exec($ curl)执行.

In addition, $curl_post_data is showing all the data by the client while debugging, before the rest call will be executed with curl_exec($curl).

可能是什么问题?

推荐答案

许多小时后,我发现了问题以及该问题的解决方案.

After lots of hours I found the problem and the solution for this problem.

问题是:

curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization));

使用"Content-Type:应用程序/json"会导致POST数据信息丢失.

Using 'Content-Type: application/json' results in loosing the POST data information.

解决方案是:

更改

'Content-Type: application/json' 

'Content-Type: application/x-www-form-urlencoded'

这篇关于Yii2 Rest身份验证承载发布数据丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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