Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

235
Views
CosmosDB REST API - Create document from node-red request node

I have a node-red flow from where I want to post a json object to Azure CosmosDB using the CosmosDB REST API. Below is a code snippet in JavaScript (node red function node) for building the POST request and as well generate the signature. I get response from Cosmos that the message is not properly formatted: "HTTP Error 400. The request is badly formed". Does anyone know what I´m doing wrong here? I´v included an image for the HTTP Request where we can see the headers and json body.

const { crypto } = context.global;
  
function getAuthorizationTokenUsingMasterKey(verb, resourceType, resourceId, date, masterKey) {  
    var key = new Buffer(masterKey, "base64");  
  
    var text = (verb || "").toLowerCase() + "\n" +   
               (resourceType || "").toLowerCase() + "\n" +   
               (resourceId || "") + "\n" +   
               date + "\n" +   
               "" + "\n";  
  
    var body = new Buffer(text, "utf8");  
    var signature = crypto.createHmac("sha256", key).update(body).digest("base64");  
  
    var MasterToken = "master";  
  
    var TokenVersion = "1.0";  
  
    return encodeURIComponent("type=" + MasterToken + "&ver=" + TokenVersion + "&sig=" + signature);  
}  

var url = "https://myurl.documents.azure.com/dbs/FamilyDatabase/colls/FamilyContainer/docs/"; //id here???
var key = "mysecretkey";
var resourceId = "FamilyContainer"; //"dbs/FamilyContainer" ??;
var authToken = getAuthorizationTokenUsingMasterKey("POST", "dbs", resourceId, new Date(), key);

msg.headers = {};

msg.headers['Authorization'] = authToken;
msg.headers['Accept'] = '*/*';
msg.headers['Accept-Encoding'] = '*/*';
msg.headers['Content-Type'] = 'application/json';
msg.headers['x-ms-documentdb-partitionkey'] = 'Andersen';
msg.headers['x-ms-version'] = "2018-12-31",
msg.headers['x-ms-date'] = new Date().toString();

msg.url = url;
msg.method = "POST";

msg.body = msg.payload;

/*
//I have tried different varaitios in the body here
msg.body = {
    "data": msg.payload
    //"id": "AndersenFamily"
}

msg.payload = {
    "data": msg.payload
    //"id": "AndersenFamily"
}
*/

return msg;

node-red POST example

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!