node-red.js HTTP POST with API key - javascript

I am trying to POST JSOn data to a cloud with this function in function node.
var accx = 15;
var accy = 45;
var accz = 12;
//JSON FORMING BY JAVASCRIPT OBJECT
var output = [];
output[0] = {
name: "Accel_X",
value: accx.toString(), // retrieve x
};
output[1] = {
name: "Accel_Y",
value: accy.toString(), // retrieve y
};
output[2] = {
name: "Accel_Z",
value: accz.toString() // retrieve z
};
var record = [];
record[0] = {
starttime: formatDate(new Date()),
output: output,
};
var observations = [];
observations[0] = {
sensor: "C2105",
record: record,
};
var fromData = {};
fromData.version = "1.0.1";
fromData.observations = observations;
//MONTH NAME FUNCTION
function show_now(){
var my_month=new Date()
var month_name=new Array(12);
month_name[0]="JAN"
month_name[1]="FEB"
month_name[2]="MAR"
month_name[3]="APR"
month_name[4]="MAY"
month_name[5]="JUN"
month_name[6]="JUL"
month_name[7]="AUG"
month_name[8]="SEP"
month_name[9]="OCT"
month_name[10]="NOV"
month_name[11]="DEC"
return month_name[my_month.getMonth()];
}
//RETURN DATE AT FORMATTED WAY THAT IS ACCEPTED BY CLOUD
function formatDate(d) {
return d.getDate() + '-' + (show_now()) + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds() + " IST";
}
var fromDatan = JSON.stringify(fromData);
//alert(fromDatan);
//POST JSON SENSOR DATA
fromDatan.headers = {
"x-api-key": "ucrFr234r23rrxfAIH2L4=",
"content-type": "application/json;charset=UTF-8"
}
return fromDatan;
I have given correct url in url node but it is returning no response and I am not seing any data is being post
Anyone please worked with node-red.js please help.

Your function node needs to return an object with a payload property containing the data you want to send in the http request. It is also not necessary to stringify the object as the http request node will do that for you.
var fromDatan = {};
fromDatan.payload = fromData;
fromDatan.headers = ...
return fromDatan

Related

Email Alert script to check for user to be notified - Google AppScript

I found a lot of useful stuff in this forum. I am new to GAS and JS coding in general and I cannot find a way to solve something that I am confident it's NBD.
I am attaching the code first:
function emailAlert() {
// today's date information
var today = new Date();
var todayMonth = today.getMonth() + 1;
var todayDay = today.getDate();
var todayYear = today.getFullYear();
// getting data from spreadsheet
var url = SpreadsheetApp.getActiveSpreadsheet().getUrl();
var sheet = SpreadsheetApp.openByUrl(url).getSheetByName("Assignments");
var resultssn = SpreadsheetApp.openByUrl(url).getName();
//Emails addresses
var emailJonDoe = "example#example.com"
var emailEmilyDoe = "example#example.com"
var duedatesRange = sheet.getRange("H5:H9");
var duedates = duedatesRange.getValues();
var actionitRange = sheet.getRange("C5:C9");
var actionit = actionitRange.getValues();
var prjsRange = sheet.getRange("B5:B9");
var prjs = prjsRange.getValues();
var whosRange = sheet.getRange("D5:D9");
var whos = actionitRange.getValues();
//looping through all of the rows
for (var i = 0; i < duedates.length; ++i) {
var row = duedates[i];
for (var i = 0; i < actionit.length; ++i) {
var assignmenttext = actionit[i];
for (var i = 0; i < prjs.length; ++i) {
var project = prjs[i];
for (var i = 0; i < whos.length; ++i) {
var user = whos[i];
var expireDateFormat = Utilities.formatDate(
new Date(row[i]),
'ET',
'MM/dd/yyyy'
);
// email information
var subject = '';
var message =
" One of your action items is due today. " +
'\n' +
'\n' +
' Project: ' +
project[i] +
'\n' +
'\n' +
' Action Item: ' +
assignmenttext[i] +
'\n' +
'\n' +
'Check the Tracker now !!' +
'\n' +
url;
//expiration date information
var expireDateMonth = new Date(row[i]).getMonth() + 1;
var expireDateDay = new Date(row[i]).getDate();
Logger.log("Expire date is:" + expireDateDay);
//check for JD and send email to him if true
if (
user[i] === "JD" &&
expireDateMonth === todayMonth &&
expireDateDay === todayDay
) {
var subject =
'FEG AAAAAAManagement - An action item is due today!! : ' + assignmenttext[i];
MailApp.sendEmail(emailJonDoe, subject, message);
Logger.log('todayyyy!');
}
//check for ED and send email to him if true
if (
user[i] === "ED" &&
expireDateMonth === todayMonth &&
expireDateDay === todayDay
) {
var subject =
'FEG DDDDDManagement - An action item is due today!! : ' + assignmenttext[i];
MailApp.sendEmail(emailEmilyDoe, subject, message);
Logger.log('todayyyy!');
}
}
}
}
}
}
enter image description here
This is a simple snippet that send emails for each row that contains today's date in column H.
I have a similar one already working but I would like to implement the "user" feature.
Long story short, I want the code to:
Check for for each row that contains today's date in column H.
For each of above rows, check the content of it's respective cell in column D.
If it contains JD, send email alert to JD's email. If it contains ED, send email alert to ED's email.
The email should contain row's respective cell for column C (assignmenttext) and column B (project).
AFAIK, this code is not too heavy on the server side since I only "getRange" at the beginning and most of the code runs on the client side (is this correct?).
I am not confident with which loop function to use in this case and how to implement it with my code.
I am open to any comment or suggestion. Thanks in advance to anyone who would spend some time to help me. (:
This is the sort of thing I would have done. Admittedly you have to be comfortable with dealing with arrays but it makes for a nice compact code.
The two objects emails and prefix provide a place to enter new users all in one place and simplify the code by allow you to use a single send command.
function emailAlert() {
const sentcolumn=45;//I recommend adding some column to contain and indication of when an email is already been sent.
const ss=SpreadsheetApp.getActive();
const sh=ss.getSheetByName("Assignments");
const vA=sh.getRange(5,1,5,sentcolumn).getValues();//your data went from row 5 to row 9 that is five rows
const dt=new Date();
const today=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();//this is a number
const emails={"JD":"jd#example.com","ED":"ed#example.com"};//emails
const prefix={"JD":"FEG AAAAAAManagement - An action item is due today!! : ","ED":"FEG DDDDDManagement - An action item is due today!! : "};//subject prefixes
vA.forEach(function(r,i){
let x=new Date(r[7]);
let duedate=new Date(x.getFullYear(),x.getMonth()+1,x.getDate()).valufOf();//this is a number
var body=Utilities.formatString('One of your action items is due today. \n\n Project: %s \n\n Action Item: %s\n\nCheck the Tracker now !!\n%s',r[1],R[2],ss.getUrl());
let subject=prefix[r[3]] + r[2];
if(today==duedate && r[sentcolumn-1]!='Sent') {
GmailApp.sendEmail(emails[r[3]],subject,body);
sh.getRange(i+5,sentcolumn).setValue('Sent');//adding something like this would keep you from sending duplicate emails by mistake
}
});
}
This could be done using a single for loop, checking data in each row:
function sendEmail() {
// today's date information
var today = new Date();
var todayMonth = today.getMonth() + 1;
var todayDay = today.getDate();
var todayYear = today.getFullYear();
Logger.log(todayDay);
// getting data from spreadsheet
var url = SpreadsheetApp.getActiveSpreadsheet().getUrl();
var sheet = SpreadsheetApp.openByUrl(url).getSheetByName("Assignments");
var resultssn = SpreadsheetApp.openByUrl(url).getName();
//Emails addresses
var emailJonDoe = "example#example.com"
var emailEmilyDoe = "example#example.com"
var lastRow = sheet.getLastRow();
var dataRange = sheet.getRange(2,1,lastRow,8).getValues();
//looping through all of the rows
for (var i = 0; i < lastRow-1; i++) {
var project = dataRange[i][1];
var assignmenttext = dataRange[i][2];
var user = dataRange[i][3];
var row = dataRange[i][7];
//expiration date information
var expireDateFormat = Utilities.formatDate(new Date(row),'ET','MM/dd/yyyy');
var expireDateMonth = new Date(row).getMonth() + 1;
var expireDateDay = new Date(row).getDate();
Logger.log("Expire date is:" + expireDateDay);
//check for expiry date
if (expireDateMonth === todayMonth && expireDateDay === todayDay) {
var subject =
'FEG AAAAAAManagement - An action item is due today!! : ' + assignmenttext;
// email information
var message =
" One of your action items is due today. " +
'\n' +
'\n' +
' Project: ' +
project +
'\n' +
'\n' +
' Action Item: ' +
assignmenttext +
'\n' +
'\n' +
'Check the Tracker now !!' +
'\n' +
url;
if (user === 'JD') {
MailApp.sendEmail(emailJonDoe, subject, message);
}
if (user === 'ED') {
MailApp.sendEmail(emailEmilyDoe, subject, message);
}
}
}
}
Running this on a sample sheet returns:
Note that I did not send emails, instead I just logged values in my test code.

How to modify response body before caching inside a service worker?

I thought it would be rather simple but I am struggle to successfully send response then modify my cloned response that is being stored in cache inside my service worker.
Ultimately I want to appended my cached data with the time and date of the fetch. I am using my service worker to first fetch this data from the network and if its not available fallback to the cache.
This is the code from my service worker
event.respondWith(
fetch(event.request)
.then(function(e) {
console.log('response', e)
var responseToCache = e.clone();
responseToCache.json().then(function(data) {
var today = new Date();
var date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
data.cached = { date: date, time: time }
return data
})
// var init = {
// status: response.status,
// statusText: response.statusText,
// headers: { 'X-Foo': 'My Custom Header' }
// };
// response.headers.forEach(function(v, k) {
// init.headers[k] = v;
// });
// var responseToCache = response.text().then(function(body) {
// var today = new Date();
// var date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
// var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
// var bodyObj = JSON.parse(body)
// bodyObj.cached = { date: date, time: time }
// body = JSON.stringify(bodyObj)
// return new Response(body, init);
// });
caches.open(CACHE_NAME)
.then(function(cache) {
cache.put(requestURL, responseToCache);
});
return e
})
.catch(function() {
console.log('data offline')
return caches.match(requestURL);
})
);

Using AWS4 Signature via Postman for CRUD Elastic operations

I'm trying to post data to Elasticsearch managed by AWS using AWS4 signing method. I would like to achieve this via postman pre-script. I tried using below script which worked perfectly for GET operation of Elastic search but its not working for POST or PUT or DELETE operation & keep giving me error message that the signature does not match for POST operation. Can someone help me in fixing below pre-script in postman?
var date = new Date().toISOString();
var amzdate = date.replace(/[:\-]|\.\d{3}/g, "");
var dateStamp = amzdate.slice(0, -8);
pm.environment.set('authorization', getAuthHeader(request.method, request.url, request.data));
pm.environment.set('xAmzDate', amzdate);
function getPath(url) {
var pathRegex = /.+?\:\/\/.+?(\/.+?)(?:#|\?|$)/;
var result = url.match(pathRegex);
return result && result.length > 1 ? result[1] : '';
}
function getQueryString(url) {
var arrSplit = url.split('?');
return arrSplit.length > 1 ? url.substring(url.indexOf('?') + 1) : '';
}
function getSignatureKey(secretKey, dateStamp, regionName, serviceName) {
var kDate = sign("AWS4" + secretKey, dateStamp);
var kRegion = sign(kDate, regionName);
var kService = sign(kRegion, serviceName);
var kSigning = sign(kService, "aws4_request");
return kSigning;
}
function sign(key, message) {
return CryptoJS.HmacSHA256(message, key);
}
function getAuthHeader(httpMethod, requestUrl, requestBody) {
var ACCESS_KEY = pm.globals.get("access_key");
var SECRET_KEY = pm.globals.get("secret_key");
var REGION = 'us-east-1';
var SERVICE = 'es';
var ALGORITHM = 'AWS4-HMAC-SHA256';
var canonicalUri = getPath(requestUrl);
var canonicalQueryString = getQueryString(requestUrl);
if (httpMethod == 'GET' || !requestBody) {
requestBody = '';
} else {
requestBody = JSON.stringify(requestBody);
}
var hashedPayload = CryptoJS.enc.Hex.stringify(CryptoJS.SHA256(requestBody));
var canonicalHeaders = 'host:' + pm.environment.get("ESHost") + '\n' + 'x-amz-date:' + amzdate + '\n';
var signedHeaders = 'host;x-amz-date';
var canonicalRequestData = [httpMethod, canonicalUri, canonicalQueryString, canonicalHeaders, signedHeaders, hashedPayload].join("\n");
var hashedRequestData = CryptoJS.enc.Hex.stringify(CryptoJS.SHA256(canonicalRequestData));
var credentialScope = dateStamp + '/' + REGION + '/' + SERVICE + '/' + 'aws4_request';
var stringToSign = ALGORITHM + '\n' + amzdate + '\n' + credentialScope + '\n' + hashedRequestData;
var signingKey = getSignatureKey(SECRET_KEY, dateStamp, REGION, SERVICE);
var signature = CryptoJS.HmacSHA256(stringToSign, signingKey).toString(CryptoJS.enc.Hex);
var authHeader = ALGORITHM + ' ' + 'Credential=' + ACCESS_KEY + '/' + credentialScope + ', ' + 'SignedHeaders=' + signedHeaders + ', ' + 'Signature=' + signature;
return authHeader;
}
The code from the OP is almost accurate just has a few bugs
1) getPath should return "/" when path=''
2) check if request.data is empty object if so requestBody = ''
3) no need to do JSON.stringify(request.data) since request.data returns a json string
The fixed snippet is below:
var date = new Date().toISOString();
var amzdate = date.replace(/[:\-]|\.\d{3}/g, "");
var dateStamp = amzdate.slice(0, -8);
pm.environment.set('authorization', getAuthHeader(request.method, request.url, request.data));
pm.environment.set('xAmzDate', amzdate);
function getPath(url) {
var pathRegex = /.+?\:\/\/.+?(\/.+?)(?:#|\?|$)/;
var result = url.match(pathRegex);
return result && result.length > 1 ? result[1] : '/';
}
function getQueryString(url) {
var arrSplit = url.split('?');
return arrSplit.length > 1 ? url.substring(url.indexOf('?') + 1) : '';
}
function getSignatureKey(secretKey, dateStamp, regionName, serviceName) {
var kDate = sign("AWS4" + secretKey, dateStamp);
var kRegion = sign(kDate, regionName);
var kService = sign(kRegion, serviceName);
var kSigning = sign(kService, "aws4_request");
return kSigning;
}
function sign(key, message) {
return CryptoJS.HmacSHA256(message, key);
}
function getAuthHeader(httpMethod, requestUrl, requestBody) {
var ACCESS_KEY = pm.globals.get("access_key");
var SECRET_KEY = pm.globals.get("secret_key");
var REGION = 'us-east-1';
var SERVICE = 'es';
var ALGORITHM = 'AWS4-HMAC-SHA256';
var canonicalUri = getPath(requestUrl);
var canonicalQueryString = getQueryString(requestUrl);
if (httpMethod == 'GET' || !requestBody || Object.keys(requestBody).length === 0) {
requestBody = '';
}
var hashedPayload = CryptoJS.enc.Hex.stringify(CryptoJS.SHA256(requestBody));
var canonicalHeaders = 'host:' + pm.environment.get("ESHost") + '\n' + 'x-amz-date:' + amzdate + '\n';
var signedHeaders = 'host;x-amz-date';
var canonicalRequestData = [httpMethod, canonicalUri, canonicalQueryString, canonicalHeaders, signedHeaders, hashedPayload].join("\n");
var hashedRequestData = CryptoJS.enc.Hex.stringify(CryptoJS.SHA256(canonicalRequestData));
var credentialScope = dateStamp + '/' + REGION + '/' + SERVICE + '/' + 'aws4_request';
var stringToSign = ALGORITHM + '\n' + amzdate + '\n' + credentialScope + '\n' + hashedRequestData;
var signingKey = getSignatureKey(SECRET_KEY, dateStamp, REGION, SERVICE);
var signature = CryptoJS.HmacSHA256(stringToSign, signingKey).toString(CryptoJS.enc.Hex);
var authHeader = ALGORITHM + ' ' + 'Credential=' + ACCESS_KEY + '/' + credentialScope + ', ' + 'SignedHeaders=' + signedHeaders + ', ' + 'Signature=' + signature;
return authHeader;
}
When setting up a CloudWatch Logs to Amazon Elasticsearch stream, AWS creates a Node.js Lambda function which does proper AWS SigV4 URL signing. Here's the relevant part from that script that you could reuse to properly generate your postman request:
function buildRequest(endpoint, body) {
var endpointParts = endpoint.match(/^([^\.]+)\.?([^\.]*)\.?([^\.]*)\.amazonaws\.com$/);
var region = endpointParts[2];
var service = endpointParts[3];
var datetime = (new Date()).toISOString().replace(/[:\-]|\.\d{3}/g, '');
var date = datetime.substr(0, 8);
var kDate = hmac('AWS4' + process.env.AWS_SECRET_ACCESS_KEY, date);
var kRegion = hmac(kDate, region);
var kService = hmac(kRegion, service);
var kSigning = hmac(kService, 'aws4_request');
var request = {
host: endpoint,
method: 'POST',
path: '/_bulk',
body: body,
headers: {
'Content-Type': 'application/json',
'Host': endpoint,
'Content-Length': Buffer.byteLength(body),
'X-Amz-Security-Token': process.env.AWS_SESSION_TOKEN,
'X-Amz-Date': datetime
}
};
var canonicalHeaders = Object.keys(request.headers)
.sort(function(a, b) { return a.toLowerCase() < b.toLowerCase() ? -1 : 1; })
.map(function(k) { return k.toLowerCase() + ':' + request.headers[k]; })
.join('\n');
var signedHeaders = Object.keys(request.headers)
.map(function(k) { return k.toLowerCase(); })
.sort()
.join(';');
var canonicalString = [
request.method,
request.path, '',
canonicalHeaders, '',
signedHeaders,
hash(request.body, 'hex'),
].join('\n');
var credentialString = [ date, region, service, 'aws4_request' ].join('/');
var stringToSign = [
'AWS4-HMAC-SHA256',
datetime,
credentialString,
hash(canonicalString, 'hex')
] .join('\n');
request.headers.Authorization = [
'AWS4-HMAC-SHA256 Credential=' + process.env.AWS_ACCESS_KEY_ID + '/' + credentialString,
'SignedHeaders=' + signedHeaders,
'Signature=' + hmac(kSigning, stringToSign, 'hex')
].join(', ');
return request;
}
function hmac(key, str, encoding) {
return crypto.createHmac('sha256', key).update(str, 'utf8').digest(encoding);
}
function hash(str, encoding) {
return crypto.createHash('sha256').update(str, 'utf8').digest(encoding);
}

Gravity forms WEBAPI javascript input name with period not working

I have a problem with a field name that has a period "." to it
I can only make it work if the field name is something like input_1 or input_1_2
But when it is something like input_1.2 it doesn't work.
function CalculateSig(stringToSign, privateKey){
//calculate the signature needed for authentication
var hash = CryptoJS.HmacSHA1(stringToSign, privateKey);
var base64 = hash.toString(CryptoJS.enc.Base64);
return encodeURIComponent(base64);
}
var d = new Date;
var expiration = 3600;
var unixtime = parseInt(d.getTime() / 1000);
var future_unixtime = unixtime + expiration;
var publicKey = "mupubkey";
var privateKey = "pyprikey";
var method = "POST";
var route = "forms/6/submissions";
stringToSign = publicKey + ":" + method + ":" + route + ":" + future_unixtime;
sig = CalculateSig(stringToSign, privateKey);
var url = 'https://www.localhostlbahblah.com/gravityformsapi/' + route + '?api_key=' + publicKey + '&signature=' + sig + '&expires=' + future_unixtime;
var values = {input_values:{'input_1.3':"test",// not working first name value
"input_2":"testetest", // this is working
}}
values_json = JSON.stringify(values);
$.post(url, values_json, function(data){
console.log(data.response);
});

How to fix this js code?

I didn't write this code, but I'm still a beginner, can anyone help me with the bugs in this? JSLint kept giving me several bugs and I couldn't seem to fix them. It would be every much appreciated. I'm learning as I post this. I'm currently working on improving my graphic/motion design skills and coding skills. But I can't write something like this currently. If there is anymore detail you need, you can just comment and ask. Thanks.
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://209.15.211.170/catalog/", false);
xhr.send();
console.log(xhr.status);
console.log(xhr.statusText);
$("html").html("<h2 style='position:absolute;left:60%;color:#BF34334;font-family:arial;'></h2>");
var minPage = 1;
var maxPage = 12;
var page = minPage;
var json = 'http://209.15.25843811.170/catalog/json?browse&Category=2';
var min = 1;
var max = Number(prompt("Maximum Robux?"));
function buy(item, price) {
"use strict";
var link = 'http://209.15.211.170/';
$.get(link, function(data) {
var info = (data).find('.ItemSalesTable'.find('.PurchaseButton')).data();
var buy = 'http://www.roblox.com/API/Item.aspx?rqtype=purchase&productID=' + info[productId] + '&expectedcurrency=1&expectedPrice=' + info[expectedPrice] + '&expectedSellerId=' + info[expectedSellerId] + & userAssetID = +info[userassetId];
if (parseInt(info) == parseInt(info['expectedPrice'])) {}
});
}
setInterval(function(3000) {
function get() {
$.get(json, function(Data) {
for (var Hat & Data {
if (max >= Price && Price > 0) {
buy(ID, Price)
var dt = new Date();
var time = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();
console.info(Name + '[' + Price + '] # ' + time);
}
}
})
}
get()
console.clear();
console.info('Running on pages ' + minPage + '-' + maxPage);
confirm = function() {};
alert = function() {};
console.clear();
Not entirely sure what your needs are, but try replacing your code with this:
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://209.15.211.170/catalog/", false);
xhr.send();
console.log(xhr.status);
console.log(xhr.statusText);
$("html").html("<h2 style='position:absolute;left:60%;color:#BF34334;font-family:arial;'></h2>");
var minPage = 1;
var maxPage = 12;
var page = minPage;
var json = 'http://209.15.25843811.170/catalog/json?browse&Category=2';
var min = 1;
var max = Number(prompt("Maximum Robux?"));
function buy(item, price) {
"use strict";
var link = 'http://209.15.211.170/';
$.get(link, function(data) {
var info = (data).find('.ItemSalesTable'.find('.PurchaseButton')).data();
var buy = 'http://www.roblox.com/API/Item.aspx?rqtype=purchase&productID=' + info[productId] + '&expectedcurrency=1&expectedPrice=' + info[expectedPrice] + '&expectedSellerId=' + info[expectedSellerId] + '&userAssetID=' +info[userassetId];
if (parseInt(info) == parseInt(info['expectedPrice'])) {}
});
};
function get() {
$.get(json, function(Data) {
for (var Hat in Data) {
if (max >= Price && Price > 0) {
buy(ID, Price);
var dt = new Date();
var time = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();
console.info(Name + '[' + Price + '] # ' + time);
}
}
});
};
setInterval(function() {
get();
console.clear();
console.info('Running on pages ' + minPage + '-' + maxPage);
confirm = function() {};
alert = function() {};
console.clear();
}, 3000);

Categories