Skip to content

Instantly share code, notes, and snippets.

@zmajstor
Created February 13, 2023 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zmajstor/a73f0641fe9b49fbdf905eb14ece6db4 to your computer and use it in GitHub Desktop.
Save zmajstor/a73f0641fe9b49fbdf905eb14ece6db4 to your computer and use it in GitHub Desktop.
Simple HTTPS Post using node:https
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