IBM Watson 的 Retrieve and Rank 服务的 API 使用基本身份验证。方法有多种,其中之一 - 使用模块request
:
var url = "https://gateway.watsonplatform.net/retrieve-and-rank/api/v1/solr_clusters/sc1ca23733_faa8_49ce_b3b6_dc3e193264c6/solr/example_collection/select?q=what%20is%20the%20basic%20mechanism%20of%20the%20transonic%20aileron%20buzz&wt=json&fl=id,title"
request.get('http://some.server.com/', {
auth: {
user: 'username',
pass: 'password',
sendImmediately: false
},
json: true
}, function(error, response, body) {
console.log( 'Found: ', body.response.numFound );
});
或者
var username = 'username',
password = 'password',
url = "https://" + user + ":" + password + "@" + "gateway.watsonplatform.net/retrieve-and-rank/api/v1/solr_clusters/sc1ca23733_faa8_49ce_b3b6_dc3e193264c6/solr/example_collection/select?q=what%20is%20the%20basic%20mechanism%20of%20the%20transonic%20aileron%20buzz&wt=json&fl=id,title"
request({url: url, json: true}, function (error, response, body) {
console.log( 'Found: ', body.response.numFound );
});