Personally, I find the http library in node to be quite low level and not very user-friendly. If you're looking for a more developer-friendly option, I would recommend checking out axios.
However, if you are determined to use the http library, the official documentation can provide guidance. Specifically, the request.write method might be what you need.
You can also refer to the same nodejs docs for some sample examples on making a post request using either axios:
const axios = require('axios')
axios
.post('https://whatever.com/todos', {
todo: 'Buy the milk'
})
.then(res => {
console.log(`statusCode: ${res.status}`)
console.log(res)
})
.catch(error => {
console.error(error)
})
or http:
const https = require('https')
const data = JSON.stringify({
todo: 'Buy the milk'
})
const options = {
hostname: 'whatever.com',
port: 443,
path: '/todos',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
}
}
const req = https.request(options, res => {
console.log(`statusCode: ${res.statusCode}`)
res.on('data', d => {
process.stdout.write(d)
})
})
req.on('error', error => {
console.error(error)
})
req.write(data)
req.end()