2016-04-02 84 views
2

我想部署一個瓶+角應用程序,其中瓶是在數字海洋Ubuntu的服務器上託管。用戶名和密碼認證角

該API基於此guide

在捲曲我可以證實它的工作原理是:curl -u <username>:<passwd> -i -X GET https://<server>

我試圖獲得相同的角度使用http:

$http.get('https://<server>', {username: <user>, password: <password>}) .then(function(response){ console.log(response.data});

結果在401未授權

我希望你可以幫助我在這裏。

+0

檢查您發送 – uksz

+0

如何檢查報頭中的標頭嗎? – DauleDK

+0

http://stackoverflow.com/questions/4423061/view-http-headers-in-google-chrome – uksz

回答

3

可以使用post方法而不是get發送數據

$http.post('https://<server>', 
    {username: <user>, password: <password>}) 
    .then(function(response){ 
     console.log(response.data); 
    }); 

和服務由req.body.username得到。我假設服務器端是'node.js',這就是爲什麼使用'req.body'的原因。

然後還需要post方法在服務器端接受您的post請求您的url

OR

如果你想使用get方法,那麼你可以發送數據爲params

var config = { 
params: {username: <user>, password: <password>}, 
headers : {'Accept' : 'application/json'} 
}; 

$http.get('https://<server>', config).then(function(response) { 
    // process response here.. 
    console.log(response.data); 
}); 
+0

後端是瓶,就像這樣[鏈接](http: //blog.miguelgrinberg.com/post/restful-authentication-with-flask)用於'Flask'的 – DauleDK

+0

使用'request.json.get('username')' –

+0

獲取數據,但是使用帖子不是「錯誤」只是因爲身份驗證,當我會使用GET沒有身份驗證? – DauleDK