1

我正在使用IBM Watson的Retrieve和Rank服务。该服务提供了一个返回搜索结果的REST API。以下是API URL如何在Node.js中调用需要用户名和密码的API

https://username:[email protected]/retrieve-and-rank/api/v1/solr_clusters/sc6e46e4f5_f30c_4a8a_ae9c_b4ab6914f7b4/solr/example-collection/select?q=some 问题&重量= JSON & FL = ID,标题,正文

由于哟可以看到这个URL发生在一个用户名和密码。 Retreive和Rank文档提到了上述调用API的模式,即将用户名和密码作为URL的一部分。如果我将它粘贴到谷歌浏览器中,则会弹出对话框,以再次输入用户名和密码。输入凭据后,我可以看到数据。

我的问题是,如何通过Node.js调用这样的URL。我不知道我从哪里开始,我应该遵循哪些步骤。

+0

我会从使用请求模块(https://www.npmjs.com/package/request)开始。只需使用您的用户名和密码从节点调用给定的url即可 – Markus

回答

2

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); 
}); 
相关问题