Working with Bing Search API


#1

I’m currently trying to work with the Bing Search API using it in React but the Bing API formatting is different from other API’s I’ve used that have required a key. This one requires a key as well but its called a subscription key the way it is set up in Node is

let bing_web_search = function (search) {
  console.log('Searching the Web for: ' + term);
  let request_params = {
        method : 'GET',
        hostname : host,
        path : path + '?q=' + encodeURIComponent(search),
        headers : {
            'Ocp-Apim-Subscription-Key' : subscriptionKey,
        }
    };

    let req = https.request(request_params, response_handler);
    req.end();
}

The above example variables like host, subscriptionKey are all defined above and this works in node but when I try doing something similar with react it doesn’t work. I’ve gone through multiple API react tutorials but I just can’t seem to figure it out.

To be fair I’ve only worked with the NYTimes API with React which is pretty straight forward. If anyone could help that would be awesome.


#2

Do you have a link to the Bing API documentation? They have multiple versions available, and I want to make sure I look at the right one :slight_smile:


#3

I sure do I’ve looked at so many examples regarding ajax, componentdidmount, fetch, and the documentation associated with all but no matter what variation I try I keep getting errors. Anyways here is the link
https://docs.microsoft.com/en-us/azure/cognitive-services/bing-web-search/quickstarts/nodejs

Thanks I really appreciate it.


#4

Try this version instead: https://docs.microsoft.com/en-us/azure/cognitive-services/bing-web-search/tutorial-bing-web-search-single-page-app

The Node version is intended for a server-side scenario. Longer term, you should move the search API call to the server, for your Bing API keys are all available in the browser for anyone to use :smile: