The following Node example creates a Url Preview for the SwiftKey Web site: https://swiftkey.com/en.
Get an access key for the free trial Cognitive Research Technologies
The following code gets URL Preview data. It is implemented in the following steps:
- Declare variables to specify the endpoint by host and path.
- Specify the query URL to preview, and add the query parameter.
- Create a handler function for the response.
- Define the Search function that creates the request and adds the Ocp-Apim-Subscription-Key header.
- Run the Search function.
The complete code for this demo follows:
'use strict';
let https = require('https');
// Replace the subscriptionKey string value with your valid subscription key.
let subscriptionKey = 'YOUR-ACCESS-KEY';
let host = 'api.labs.cognitive.microsoft.com';
let path = '/urlpreview/v7.0/search';
let mkt = 'en-US';
let q = 'https://swiftkey.com/';
let params = '?q=' + encodeURI(q);
let response_handler = function (response) {
let body = '';
response.on('data', function (d) {
body += d;
});
response.on('end', function () {
let body_ = JSON.parse(body);
let body__ = JSON.stringify(body_, null, ' ');
console.log(body__);
});
response.on('error', function (e) {
console.log('Error: ' + e.message);
});
};
let Search = function () {
let request_params = {
method: 'GET',
hostname: host,
path: path + params,
headers: {
'Ocp-Apim-Subscription-Key': subscriptionKey,
}
};
let req = https.request(request_params, response_handler);
req.end();
}
Search();