Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// required node.js libraries
const request = require('request');
const crypto = require('crypto');
// Azure Log Analysis credentials
const workspaceId = 'xxxxx';
const sharedKey = 'xxxx';
// Specify the name of the record type that you'll be creating
const LogType = "BoxReports"
let apiVersion = '2016-04-01';
let processingDate = new Date().toUTCString();
let body = JSON.stringify([
{
//some json data
}
]
);
console.log('Body is ' + body);
var contentLength = Buffer.byteLength(body, 'utf8');
var stringToSign = 'POST\n' + contentLength + '\napplication/json\nx-ms-date:' + processingDate + '\n/api/logs';
var signature = crypto.createHmac('sha256', new Buffer(sharedKey, 'base64')).update(stringToSign, 'utf-8').digest('base64');
var authorization = 'SharedKey ' + workspaceId + ':' + signature;
var headers = {
"content-type": "application/json",
"Authorization": authorization,
"Log-Type": LogType,
"x-ms-date": processingDate
};
console.log( 'Request Headers: ' + JSON.stringify(headers) );
var url = 'https://' + workspaceId + '.ods.opinsights.azure.com/api/logs?api-version=' + apiVersion;
request.post({url: url, headers: headers, body: body}, function (error, response, body) {
console.log('error:', error);
console.log('statusCode:', response && response.statusCode);
console.log('body:', body);
console.res = { 'status': response.statusCode, 'body': body }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.