2016-08-19 337 views
0

我正在向我創建的API端點發送PUT請求。使用jwt,我可以成功註冊並取回令牌。智威湯遜:爲什麼我總是得到token_not_provided?

使用郵遞員,我的請求完美地工作。

我在我的應用程序中使用Guzzle發送PUT請求。這是什麼樣子:

$client = new \Guzzle\Http\Client('http://foo.mysite.dev/api/'); 
$uri = 'user/123'; 
$post_data = array(
    'token' => eyJ0eXAiOiJKV1QiLCJhbGc..., // whole token 
    'name' => 'Name', 
    'email' => [email protected], 
    'suspended' => 1, 
); 

$data = json_encode($post_data); 

$request = $client->put($uri, array(
    'content-type' => 'application/json' 
)); 

$request->setBody($data); 
$response = $request->send(); 
$json = $response->json(); 

} catch (\Exception $e) { 
    error_log('Error: Could not update user:'); 
    error_log($e->getResponse()->getBody());    
} 

當我登錄的$data變量,看看它是什麼樣子,這是返回什麼。

error_log(print_r($data, true)); 

{"token":"eyJ0eXAiOiJKV1QiL...","name":"Name","email":"[email protected]","suspended":1} 

Error: Could not suspend user: 
{"error":"token_not_provided"} 

看起來好像所有數據都正確填充,我不知道系統爲什麼沒有找到令牌。通過Postman運行「相同」查詢(作爲PUT)以及相同的參數運行效果很好。

任何建議,非常感謝!

回答

0

令牌應該在授權報頭中設置,而不是作爲後數據參數

$request->addHeader('Authorization', 'Basic eyJ0eXAiOiJKV1QiL...');