Yii2 Rest Authentication Bearer Post Data missing

前端 未结 2 949
遇见更好的自我
遇见更好的自我 2020-12-21 23:20

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. T

相关标签:
2条回答
  • 2020-12-22 00:10

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

    The problem is:

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

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

    Solution for this is:

    change

    'Content-Type: application/json' 
    

    to

    'Content-Type: application/x-www-form-urlencoded'
    
    0 讨论(0)
  • 2020-12-22 00:21

    You can use yii2's HttpBasicAuth authenticator behaviour for bearer token auth.

    use yii\filters\auth\HttpBasicAuth;
    
    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['authenticator'] = [
            'class' => HttpBasicAuth::className(),
        ];
        return $behaviors;
    }
    

    Authentication

    For the solution you were suggesting, you should use json request parser.

    'request' => [
      'parsers' => [
        'application/json' => 'yii\web\JsonParser',
      ]
    ]
    

    JsonParser

    0 讨论(0)
提交回复
热议问题