Skip to content

Instantly share code, notes, and snippets.

@gowrishankarin
Last active September 26, 2016 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gowrishankarin/2712cce2ada025f7a396c2885d33adc2 to your computer and use it in GitHub Desktop.
Save gowrishankarin/2712cce2ada025f7a396c2885d33adc2 to your computer and use it in GitHub Desktop.
const fs = require('fs');
const http = require('http');
const crypto = require('crypto');
const image_location = 'some-image-in-current-folder.jpg';
const config = {
recognizeIm: {
clientId: '',
apiKey: '',
clAPIKey: '',
hostname: 'clapi.itraff.pl',
port: '80',
version: '/v2'
}
};
fs.readFile(image-location, (err, data) => {
if(err)
throw err;
console.log(data);
var base64Image = new Buffer(data).toString('base64');
var hash = crypto.createHash('md5')
.update(config.recognizeIm.apiKey)
.update(base64Image)
.digest('hex');
console.log(hash);
var options = {
hostname: config.recognizeIm.hostname,
port: config.recognizeIm.port,
path: config.recognizeIm.version + '/recognize/' + 'shelf/' + config.recognizeIm.clientId,
method: 'POST',
headers: {
'Content-Type': 'image/jpeg',
'x-itraff-hash': hash
}
};
var request = http.request(options, (response) => {
console.log('STATUS: ' + response.statusCode);
console.log('HEADERS: ' + JSON.stringify(response.headers));
response.setEncoding('utf8');
response.on('data', (chunk) => {
console.log('BODY: ' + chunk);
});
response.on('end', () => {
console.log('End of Response');
})
});
request.on('error', (e) => {
console.log('problem with request: ' + e);
});
request.write(base64Image);
request.end();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment