Skip to content

Instantly share code, notes, and snippets.

@ailabs-software
Created December 8, 2019 22:43
Show Gist options
  • Save ailabs-software/80dabe4222e9bdf7b65322c7ef9aa5df to your computer and use it in GitHub Desktop.
Save ailabs-software/80dabe4222e9bdf7b65322c7ef9aa5df to your computer and use it in GitHub Desktop.
Use Bandwindow API from Node.js: JSON POST
var https = require("https");
function callBandWindowAPI( requestOptions, data ) {
return new Promise((resolve, reject) => {
var postReq = https.request(
requestOptions,
function(response) { // console.log('\n\nin fun...', response);
const { statusCode } = response;
if (statusCode >= 300) {
reject(
new Error( response.statusMessage )
);
}
const chunks = [];
response.on('data', (chunk) => {
chunks.push(chunk);
});
response.on('end', () => {
const result = Buffer.concat(chunks).toString();
resolve( JSON.parse(result) );
});
}
);
if (data != null) {
postReq.write( JSON.stringify(data) );
}
postReq.end();
})
}
const api_host = "YOUR_ACCOUNT.shopwindow.io";
const auth_un = "YOUR_USER";
const auth_pw = "YOUR_PASSWORD";
function addPersonToGroup()
{
callBandWindowAPI(
{
host: api_host,
path: '/dg3_rpchost/Zealand/crm/DataAPI/GroupAPI/GroupAPI/addPerson',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + new Buffer( auth_un + ':' + auth_pw ).toString('base64')
},
},
// data:
{
"group_id": "YOUR_GROUP_ID",
"person_id": "YOUR_PERSON_ID"
}
)
.then(
response => {
console.log(response);
}
)
.catch(
error => {
console.log(error);
}
);
}
addPersonToGroup();
@ailabs-software
Copy link
Author

James,
Here's a tip:
Always send a JSON body in your POST, even if you have nothing to send. You can just send "{}" in that case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment