2016-11-20 97 views
0

我已將AWS API網關與AWS Cognito用戶池集成。以下curl命令起作用。AWS API網關授權失敗

curl -X PUT -H "Authorization:<id token>" -d <json data> <https url> 

但是,以下Angularjs $ http.put授權失敗。 AWS CloudWatch的日誌顯示「用戶是未經授權的」

$http.defaults.headers.common['Authorization'] = token 
$http.put('https://<url>', <json data>) 
    .then(function (data, status, headers, config) { 
     console.log('success') 
    }, function (data, status, headers, config) { 
     console.log('failure') 
    }); 

應該如何授權令牌可以用$ http.put設置?

回答

0

我認爲你想要做的是將請求頭設置爲$http.put(<url>, options)...中的第二個參數。

所以你的情況,你的代碼應該是這樣的:

var options = {headers: { 
         'Authorization': token, 
         'Content-Type': 'application/json' 
         } 
       }; 

$http.put('https://<url>', <json data>, options); 

您可以檢查documentation here瞭解更多關於調用$http.X方法的不同方式。

我希望這會有所幫助。