Sergio
Sergio

Reputation: 73

Send unparsed data in a Node JS request

I want to send a POST request (for example, with the 'request' module), but I don't find a way of sending unparsed data*.

*unparsed data => copied directly from the Chrome dev tool. Something like: tipo_accion=3&filtro=descarga&fecha=actual

It would also do the trick some way of translating that string to JSON.

I've tried so far with...

var request = require('request');

request.post({ url: 'https://target.com/form/', form: 'tipo_accion=3&filtro=descarga&fecha=actual' },
    function (error, response, body) {
        console.log(body)
    }
);

... but it didn't work.

Upvotes: 1

Views: 86

Answers (2)

盧彥丞
盧彥丞

Reputation: 1

You can convert form_data to string format and it works for me you can try it :

const request = require('request-promise'); 
var j = request.jar()

var data_fom = `a=value_a&b=value_b&c=value_c`
request({ 
        url:"https://target.com/form/",
        jar:j,
        method:"POST",
        resolveWithFullResponse: true,
        form:data_form
    })

Upvotes: 0

Alexandru Olaru
Alexandru Olaru

Reputation: 7092

Firstly you should understand the difference between the request methods post and get.

The structure that you want to send: tipo_accion=3&filtro=descarga&fecha=actual is telling me that you want to use a get request. So the proper code for that will be something like:

request(
  'https://target.com/form/&tipo_accion=3&filtro=descarga&fecha=actual',
  function (error, response, body) {
    console.log(body)
  },
);

But if it is a post request then you should use the json format

request.post({
  url: 'https://target.com/form/', form: {
    tipo_accion: 3,
    filtro: 'descarga',
    fecha: 'actual'
  },
  function(error, response, body) {
    console.log(body)
  }
);

Upvotes: 1

Related Questions