Skip to content

Commit bf0bd9d

Browse files
authored
Updated library
1 parent c0dccd0 commit bf0bd9d

File tree

1 file changed

+33
-53
lines changed

1 file changed

+33
-53
lines changed

nodejs/Search/BingImageSearchv7.js

Lines changed: 33 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,41 @@
1-
//Copyright (c) Microsoft Corporation. All rights reserved.
2-
//Licensed under the MIT License.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
33

44
'use strict';
55

6-
let https = require('https');
6+
let request = require('request');
77

8-
// **********************************************
9-
// *** Update or verify the following values. ***
10-
// **********************************************
8+
/**
9+
* This sample uses the Bing Image Search API to query a search topic
10+
* and return image results for that topic, along with metadata.
11+
*/
1112

12-
// Add your Bing Search V7 subscription key to your environment variables.
13+
// Add your Bing Search V7 subscription key and endpoint to your environment variables.
1314
let subscriptionKey = process.env['BING_SEARCH_V7_SUBSCRIPTION_KEY']
14-
15-
// Add your Bing Search V7 endpoint to your environment variables.
16-
let host = process.env['BING_SEARCH_V7_ENDPOINT']
17-
let path = '/bing/v7.0/images/search';
18-
19-
let term = 'puppies';
20-
21-
let response_handler = function (response) {
22-
let body = '';
23-
response.on('data', function (d) {
24-
body += d;
25-
});
26-
response.on('end', function () {
27-
console.log('\nRelevant Headers:\n');
28-
for (var header in response.headers)
29-
// header keys are lower-cased by Node.js
30-
if (header.startsWith("bingapis-") || header.startsWith("x-msedge-"))
31-
console.log(header + ": " + response.headers[header]);
32-
body = JSON.stringify(JSON.parse(body), null, ' ');
33-
console.log('\nJSON Response:\n');
34-
console.log(body);
35-
});
36-
response.on('error', function (e) {
37-
console.log('Error: ' + e.message);
38-
});
39-
};
40-
41-
let bing_image_search = function (search) {
42-
console.log('Searching images for: ' + term);
43-
let request_params = {
44-
method : 'GET',
45-
hostname : host,
46-
path : path + '?q=' + encodeURIComponent(search),
47-
headers : {
48-
'Ocp-Apim-Subscription-Key' : subscriptionKey,
49-
}
50-
};
51-
52-
let req = https.request(request_params, response_handler);
53-
req.end();
15+
let endpoint = process.env['BING_SEARCH_V7_ENDPOINT'] + '/bing/v7.0/images/search';
16+
17+
let query = 'puppies';
18+
let mkt = 'en-US'
19+
20+
// Construct parameters
21+
let request_params = {
22+
method: 'GET',
23+
uri: endpoint,
24+
headers: {
25+
'Ocp-Apim-Subscription-Key': subscriptionKey
26+
},
27+
qs: {
28+
q: query,
29+
mkt: mkt
30+
},
31+
json: true
5432
}
5533

56-
if (subscriptionKey.length === 32) {
57-
bing_image_search(term);
58-
} else {
59-
console.log('Invalid Bing Search API subscription key!');
60-
console.log('Please paste yours into the source code.');
61-
}
34+
// Make request
35+
request(request_params, function (error, response, body) {
36+
console.error('error:', error)
37+
console.log('statusCode:', response && response.statusCode)
38+
console.log('original query: ' + body.queryContext.originalQuery)
39+
console.log()
40+
console.log(body)
41+
})

0 commit comments

Comments
 (0)