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

如何在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.


原文:https://stackoverflow.com/questions/35599966
更新时间:2023-01-08 10:01

最满意答案

除非你想要做的只是迭代一次结果,或者你需要一个可重用的实时过滤视图,你可能需要一个包含匹配的非视图列表。 创建一个ListSet来存储结果,然后迭代数据列表并添加匹配,这是一个非常好的方法,并且易于理解!

List<Data> result = Lists.newArrayList();
for (Data data : data1) {
  if (dataIds.contains(data.getId()))
    result.add(data);
}

我看到你的Data类实现了一个可Identifiable接口。 鉴于此,您可以创建一个Function<Identifiable, Integer>来获取ID ... Identifiables.getIdFunction()或其他东西。 这很好,因为它可能在其他地方很有用(我在这里的博客文章中讨论了这种方法)。 有了这个,用番石榴做这件事也相当简单:

Predicate<Identifiable> predicate = Predicates.compose(
    Predicates.in(dataIds), Identifiables.getIdFunction());
List<Data> filtered = Lists.newArrayList(Iterables.filter(data1, predicate));

这基本上在功能上与第一个例子相同,但似乎很难理解。 由于这样做没有明显的好处(与您只想使用实时视图的情况不同),所以我的建议是先按照第一步进行。


Unless all you want to do is iterate through the result once or you need a reusable live filtered view, you probably want a non-view list containing the matches. Creating a List or Set to store the result and then iterating through the data list and adding matches is a perfectly good approach and easy to understand!

List<Data> result = Lists.newArrayList();
for (Data data : data1) {
  if (dataIds.contains(data.getId()))
    result.add(data);
}

I see your Data class implements an Identifiable interface. Given that, you could create a Function<Identifiable, Integer> that gets the ID... Identifiables.getIdFunction() or something. This is nice because it'd likely be useful in various other places (I talk about that approach in a blog post here). With that in place, doing this with Guava would be fairly simple as well:

Predicate<Identifiable> predicate = Predicates.compose(
    Predicates.in(dataIds), Identifiables.getIdFunction());
List<Data> filtered = Lists.newArrayList(Iterables.filter(data1, predicate));

This is basically functionally equivalent to the first example, but seems like it'd be harder to understand. Since there isn't any clear benefit to doing this (unlike in a situation where you want to just use the live view), my recommendation would be to just go with the first.

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)