Created
February 13, 2023 16:38
-
-
Save zmajstor/a73f0641fe9b49fbdf905eb14ece6db4 to your computer and use it in GitHub Desktop.
Simple HTTPS Post using node:https
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
const NODE_HTTPS = require('node:https') | |
const NODE_URL = require('node:url') | |
// resolves to response status code is success | |
const simpleApiPost = ({ endpointURL, body, bearerToken }) => new Promise((resolve, reject) => { | |
try { | |
const options = { | |
...NODE_URL.parse(endpointURL), | |
port: 443, | |
method: 'POST', | |
headers: { | |
'Authorization': `Bearer ${bearerToken}`, | |
'Accept': 'application/json', | |
'Content-Type': 'application/json' | |
} | |
} | |
const req = NODE_HTTPS.request(options, (res) => { | |
res.on('end', () => resolve(res.statusCode)) | |
}) | |
req.on('error', e => reject(e)) | |
req.write(JSON.stringify(body)) | |
req.end() | |
} catch (err) { | |
reject(err) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment