Created
December 8, 2019 22:43
-
-
Save ailabs-software/80dabe4222e9bdf7b65322c7ef9aa5df to your computer and use it in GitHub Desktop.
Use Bandwindow API from Node.js: JSON POST
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.