0

I'm using a platform for data security, and they have this code snippet showing how to post data to their platform:

They're using the request module: https://github.com/mikeal/request

const request = require('request');

request({
    url: 'https://mypass.testproxy.com/post',
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify({'secret' : 'secret_value'})
  }, function(error, response, body){
    if(error) {
      console.log(error);
    } else {
      console.log('Status:', response.statusCode);
      console.log(JSON.parse(body));
    }
});

It works fine, but I wanted to replace the 'secret' : 'secret_value' object with my form data, but I'm having a hard time figuring out how to do this. The only way I know how to retrieve form data is with req.body:

function(req, res) {
var form = {
  card_number: req.body.card_number,
  card_cvv: req.body.cvv,
  card_expirationDate: req.body.card_expirationDate,
};
    // ...
});

How would I do that? Any help is greatly appreciated.

I know the code below is wrong, but that's the idea of what I want to achieve:

request( function(req, res) {
var form = {
  card_number: req.body.card_number,
  card_cvv: req.body.cvv,
  card_expirationDate: req.body.card_expirationDate,
};{
    url: 'https://mypass.testproxy.com/post',
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(form)
...```



1 Answer 1

1

The form data will be sent with the content type application/x-www-form-urlencoded e.g. card_number=123&cvv=456.

Express has a middleware to parse that https://expressjs.com/en/api.html#express.urlencoded

app.use(express.urlencoded());

The parsed values will be in req.body in your post route. So e.g. req.body.card_number will contain the value 123.

You can put the request inside the route:

app.post('/', function (req, res) {
  var form = { /* ... */ }
  request({
    body: JSON.stringify(form),
    /* ... */
  })
})
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.