我正在使用node-rest-client庫調用我的rest API,在測試服務器中運行python flask應用程序。節點設置和請求代碼如下。基本身份驗證在node-rest-client節點中不起作用
如果我添加一個用戶標記到請求標頭它的話很好,但在調用獲得用戶標記使用基本身份驗證我的Python系統成功驗證並返回與200狀態的標記,但瓶服務器是然後將其更改爲400錯誤請求。它使用郵差工作很好。
有沒有從我的兩個配置對象的節點休息客戶端?
乾杯。
var options_auth = {
user: "bob",
password: "password",
mimetypes: {
json: ["application/json", "application/json;charset=utf-8"]
}
};
var Client = require('node-rest-client').Client;
var client = new Client(options_auth);
var method = "/authtoken";
var args = {
headers: {
"Content-Type": "application/json",
"api-key": "asf89a7assa98d7sd98d98ds",
//"token": "dsf98s7dsf98dsf7sd98f7dsf",
"Accept": "application/json"
},
responseConfig: {
timeout: 1000 //response timeout
}
};
client.get(Api.url+method, args, function (data, response) {
// parsed response body as js object
// raw response
//console.log(response);
if(Buffer.isBuffer(data)){
data = data.toString('utf8');
}
console.log(data);
var stringData = data.toString('utf8');
console.log("String data = "+stringData);
}).on('error', function (err) {
console.error('Something went wrong with the http client', err);
});
另外,發現由服務器接收到的請求報頭之間的這些差異:
// Node Request fails: 400
'headers': EnvironHeaders([
('Authorization', u'Basic XXXXXXXXXXXXXX=='),
('Vga-Api-Key', u'xxxxxxxxxxxxxxxxxxxxxxxx'),
('Content-Length', u'0'),
('Connection', u'close'),
('Host', u'127.0.0.1:8080'),
('Accept', u'*/*'),
('Content-Type', u'application/json')]),
// Postman Request works: 200
'headers': EnvironHeaders([
('Authorization', u'Basic XXXXXXXXXXXXXX=='),
('Vga-Api-Key', u'xxxxxxxxxxxxxxxxxxxxxxxx'),
* ('Content-Length', u''),
* ('Connection', u'keep-alive'),
('Host', u'127.0.0.1:8080'),
* ('Cache-Control', u'no-cache'),
('Accept', u'*/*'),
('Content-Type', u''),
* ('Accept-Encoding', u'gzip, deflate')]),
嗨,不,我沒有在請求體中傳遞任何東西,所以我沒有在燒瓶端提取JSON。其實我只是試圖嘗試修復這個問題:-) – Jon
其實我只是通過刪除一些不需要的操作來完成這個工作,現在它已經不需要了,而且它工作了 - 我嘗試了另外一個函數來提取用戶數據和它工作得很好。所以也許這是一種時間問題?它的奇怪,也許我會再次遇到這個問題,但現在它的工作。儘管我不會說我已經解決了這個問題,但現在卻只是側重於這個問題。另外,我對Node很陌生,但在標準的http客戶端上使用node-rest-client沒有太大的收穫。 – Jon
@Jon:你刪除了什麼? – mhawke