如何在Node.js中调用需要用户名和密码的API(How to call API that requires user name and password, in Node.js)

编程入门 行业动态 更新时间:2024-10-25 07:18:25
如何在Node.js中调用需要用户名和密码的API(How to call API that requires user name and password, in Node.js)

我正在使用IBM Watson的Retrieve和Rank服务。 此服务提供返回搜索结果的REST API。 以下是API URL

https:// username:password@gateway.watsonplatform.net/retrieve-and-rank/api/v1/solr_clusters/sc6e46e4f5_f30c_4a8a_ae9c_b4ab6914f7b4/solr/example-collection/select?q = some question&wt = json&fl = id,title,body

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

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

I am working with Retrieve and Rank service of IBM Watson. This service provides a REST API that returns search result. Following is the API URL

https://username:password@gateway.watsonplatform.net/retrieve-and-rank/api/v1/solr_clusters/sc6e46e4f5_f30c_4a8a_ae9c_b4ab6914f7b4/solr/example-collection/select?q=some question&wt=json&fl=id,title,body

As yo can notice this URL takes in a user name and a password. The Retreive and Rank documentation mentions the above pattern for calling the API, i.e, with user name and password as part of the URL. If I paste this in google chrome, it comes out with dialog box to enter user name and password again. After I enter the credentials I can see the data.

My question is, how do I call such a URL through Node.js. I do not know where do I start and what steps I should follow.

最满意答案

IBM Watson的Retrieve和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 ); });

API of Retrieve and Rank service of IBM Watson uses basic authentication. Ways are several, one of them - use the module 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 ); });

or

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 ); });

更多推荐

本文发布于:2023-04-27 15:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1327139.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:用户名   密码   如何在   js   Node

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!