2

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.

ralphearle
  • 1,698
  • 11
  • 18
KurioZ7
  • 5,056
  • 6
  • 36
  • 58
  • I would start by using the request module (https://www.npmjs.com/package/request). just call the given url from node with your username and password – Markus Feb 24 '16 at 11:22

2 Answers2

3

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 );
});
stdob--
  • 25,371
  • 5
  • 48
  • 60
1

IBM Watson has a node SDK you might want to use in your app: https://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/retrieve-and-rank/api/v1/?node#

var watson = require('watson-developer-cloud');
var retrieve_and_rank = watson.retrieve_and_rank({
  username: '{username}',
  password: '{password}',
  version: 'v1'
});
Frederic Lavigne
  • 646
  • 3
  • 10