如何在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
最满意答案
除非你想要做的只是迭代一次结果,或者你需要一个可重用的实时过滤视图,你可能需要一个包含匹配的非视图列表。 创建一个
List
或Set
来存储结果,然后迭代数据列表并添加匹配,这是一个非常好的方法,并且易于理解!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
orSet
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 anIdentifiable
interface. Given that, you could create aFunction<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.
相关问答
更多-
基于其他列表属性集合从List
中提取对象(Extracting objects from List [2022-05-09]based on some other list property collection) 除非你想要做的只是迭代一次结果,或者你需要一个可重用的实时过滤视图,你可能需要一个包含匹配的非视图列表。 创建一个List或Set来存储结果,然后迭代数据列表并添加匹配,这是一个非常好的方法,并且易于理解! List result = Lists.newArrayList(); for (Data data : data1) { if (dataIds.contains(data.getId())) result.add(data); } 我看到你的Data类实现了一个可Ident ... -
如何通过集合中对象的属性对List进行排序?(How do I sort the List by a property of the Objects in the collection?)[2022-06-22]
例如,按位置排序,您可以使用 java.util.Collections.sort(tasks, new Comparator() { @Override public int compare(Task t1, Task t2) { return t1.getPosition() - t2.getPosition(); }); Yuval = 8-) To sort by position, for example, you can use java.util.Collections. ... -
使用Linq获取基于其他集合的属性的集合(Get a collection based on the property of an other collection with Linq)[2023-10-26]
所以我的想法是首先得到一个ItemSupplier的列表,然后使用Any()获取Item列表 为什么你想这样做,如果你可以通过一个单一的LINQ to Entities查询得到所需的结果,像这样: itemList = context.Items.Where(item => db.ItemSupplier.Any(supplier => supplier.ItemCode == item.ItemCode && supplier.AccountCode == code)) .ToList() ... -
覆盖MyClass equals和hashCode以满足您的相等要求。 这是一个例子: public boolean equals(Object o) { if( !(o instanceof MyClass) ) return false; MyClass mc = (MyClass)o; if(this.someInt != mc.someInt) return false; return (this.someString == null) ...
-
好吧,这个操作本质上是线性的,除非你根据你希望以这种方式检查的属性做一些极端的事情,比如索引集合。 除此之外,您只需要查看集合中的每个对象。 但是,您可以采取一些措施来提高可读性。 例如,Groovy为集合提供了each()方法 。 它可以让你做这样的事...... def returnList = new ArrayList(); users.each() { if ("springfield".equalsIgnoreCase(it.getHomeTown()) returnL ...
-
var newList = source.Select(item=>item.TypeId).ToList(); var newList = source.Select(item=>item.TypeId).ToList();
-
一种缓慢而清晰的方式是 var matched = cars.Where(car => intList.Contains(car.id)).ToList(); 您可以通过将intList转换为字典并使用ContainsKey来更快地实现这一点。 var intLookup = intList.ToDictionary(k => k); var matched = cars.Where(car => intLookup.ContainsKey(car.id)).ToList(); 更好的是,一个HashS ...
-
将列表转换为基于布尔属性的集合的最简单方法?(Simplest way to convert a list to a set based on a boolean property?)[2022-10-05]
对于这个简单的东西,只需编写一个循环并完成它。 如果这是一种模式,你将一遍又一遍地实施: (未经测试,所以可能有一两个错字) public interface Selector{ public boolean select(T t); } public Set grep(Collection coll, Selector sel) { Set result = new Set ; for (T item : coll) i ... -
ListOfC.Select(c => c.name).ToList(); Select是一个转换函数,它根据给定的选择器返回一个新的IEnumerable 。 由于您特别需要List ,因此需要将其转换为List 。 ListOfC.Select(c => c.name).ToList(); Select is a transform function, it returns a new IEnumerable based on the selector it is given. As you spe ...
-
EF创建的类都是PARTIAL类,这意味着您可以向它们添加更多属性并在程序中使用它们而不会影响数据库。 您可以轻松扩展这样的类: namespace Solution.Project.Model { partial class Title { public bool IsChecked {get;set;} } } 编辑:记住命名空间! The classes which EF creates are all PARTIAL classes, which means you can a ...