Pass parameters via POST with Node.js

Asked

Viewed 809 times

2

I’m trying to access a service that returns an XML Description of service and there it says that I have to inform some parameters via POST. It’s the first time I’m having to do this and I’m not getting it. Someone can give a light?

1 answer

1

Using pure Node only, you can do something like this:

const querystring = require('querystring')
const https = require('https')
const postData = querystring.stringify({
  txtLogin: 'login',
  txtSenha: 'senha',
  txtData: 'data'
})

const options = {
  hostname: 'www.rad.cvm.gov.br',
  port: 80,
  path: '/DOWNLOAD/SolicitaDownload.asp',
  method: 'POST'
}

const req = https.request(options, (res) => {
  res.setEncoding('utf8')
  res.on('data', (chunk) => {
    console.log(`Corpo: ${chunk}`);
  })
  res.on('end', () => {
    console.log('Nenhum dado a mais na resposta.');
  })
})

req.on('error', (e) => {
  console.error(`Problemas no request: ${e.message}`)
})

req.write(postData)
req.end()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.