-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
64 lines (52 loc) · 1.81 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
https = require('https');
module.exports = function (context, req) {
context.log('***************************** Start Run ****************************')
if (req.query.imageURL || (req.body && req.body.imageURL)) {
var url = req.body.imageURL;
postContent(url, context);
} else {
context.res = {
status: 400,
body: "Please pass an imageURL on the query string or in the request body"
};
context.done();
}
};
// make request to Cognitive Services
const postContent = function(url, context) {
var postData = JSON.stringify({'url': url});
var options = {
hostname: 'westcentralus.api.cognitive.microsoft.com',
port: 443,
path: '/vision/v1.0/describe',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': process.env.API_KEY
}
};
var req = https.request(options, (res) => {
context.log(`STATUS: ${res.statusCode}`);
res.setEncoding('utf8');
res.on('data', (chunk) => {
context.log(`BODY: ${chunk}`);
var result = JSON.parse(chunk).description.captions[0].text;
context.log(result);
context.res = {
body: result
};
});
res.on('end', () => {
context.log('***************************** End Run ****************************');
context.done();
});
});
req.on('error', (e) => {
context.log(`problem with request: ${e.message}`);
context.log('***************************** End Run ****************************');
context.done();
});
// write data to request body
req.write(postData);
req.end();
};