121

I have code like the following:

var req = require('request');

req.post('someUrl',
   { form: { username: 'user', password: '', opaque: 'someValue', logintype: '1'}, },
   function (e, r, body) {
      console.log(body);
});

How can I set headers for this? I need user-agent, content-type and probably something else to be in the headers:

headers = { 
   'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36',
   'Content-Type' : 'application/x-www-form-urlencoded' 
};

I've tried in multiple ways but I can either send header or form-data, failed to send both.

simhumileco
  • 31,877
  • 16
  • 137
  • 115
Mike G.
  • 3,860
  • 3
  • 17
  • 18

5 Answers5

210

I've finally managed to do it. Answer in code snippet below:

var querystring = require('querystring');
var request = require('request');

var form = {
    username: 'usr',
    password: 'pwd',
    opaque: 'opaque',
    logintype: '1'
};

var formData = querystring.stringify(form);
var contentLength = formData.length;

request({
    headers: {
      'Content-Length': contentLength,
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    uri: 'http://myUrl',
    body: formData,
    method: 'POST'
  }, function (err, res, body) {
    //it works!
  });
c24w
  • 7,421
  • 7
  • 39
  • 47
Mike G.
  • 3,860
  • 3
  • 17
  • 18
  • throw new Error('undefined is not a valid uri or options object.') ^ Error: undefined is not a valid uri or options object. at request (C:\Users\pjt\node_modules\request\index.js:44:11) at Request._callback (C:\Users\pjt\routes\payment.js:170:11) at Request.self.callback (C:\Users\pjt\node_modules\request\request.js:186:22) at emitTwo (events.js:106:13) at Request.emit (events.js:194:7) at Request. (C:\Users\pjt\node_modules\request\request.js:1163:10) at emitOne (events.js:96:13) at Request.emit (events.js:191:7) – Tamilselvan K Mar 25 '18 at 08:33
  • This didn't work for me where the answer below which just sets an object to form did. – ozzieisaacs Jun 11 '18 at 15:35
  • 1
    its deprecated https://www.npmjs.com/package/request – Andriy Antonov Jan 15 '21 at 11:42
55

This should work.

var url = 'http://<your_url_here>';
var headers = { 
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0',
    'Content-Type' : 'application/x-www-form-urlencoded' 
};
var form = { username: 'user', password: '', opaque: 'someValue', logintype: '1'};

request.post({ url: url, form: form, headers: headers }, function (e, r, body) {
    // your callback body
});
user606521
  • 14,486
  • 30
  • 113
  • 204
  • Thanks, but seems in this case form data is not sent in a proper way. I have almost the same code in .Net and in case form data is sent I should not have login form in body and should have token. I'll post it here soon, probably it will help – Mike G. Jun 17 '13 at 06:32
  • 2
    `request` is deprecated... https://www.npmjs.com/package/request – Andriy Antonov Jan 15 '21 at 11:42
21

I think it's just because you have forgot HTTP METHOD. The default HTTP method of request is GET.

You should add method: 'POST' and your code will work if your backend receive the post method.

var req = require('request');

req.post({
   url: 'someUrl',
   form: { username: 'user', password: '', opaque: 'someValue', logintype: '1'},
   headers: { 
      'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36',
      'Content-Type' : 'application/x-www-form-urlencoded' 
   },
   method: 'POST'
  },

  function (e, r, body) {
      console.log(body);
  });
nodejh
  • 7,928
  • 1
  • 20
  • 24
10

I found the solution to this problem and It should work. I'm sure about this because I also faced the same problem

here is my solution----->

var request = require('request');

//set url
var url = 'http://localhost:8088/example';

//set header
var headers = {
    'Authorization': 'Your authorization'
};

//set form data
var form = {first_name: first_name, last_name: last_name};

//set request parameter
request.post({headers: headers, url: url, form: form, method: 'POST'}, function (e, r, body) {

    var bodyValues = JSON.parse(body);
    res.send(bodyValues);
});
dan1st
  • 12,568
  • 8
  • 34
  • 67
Chetna Joshi
  • 141
  • 1
  • 10
6

Just remember set method to POST in options. Here is my code

var options = {
    url: 'http://www.example.com',
    method: 'POST', // Don't forget this line
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'X-MicrosoftAjax': 'Delta=true', // blah, blah, blah...
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36',
    },
    form: {
        'key-1':'value-1',
        'key-2':'value-2',
        ...
    }
};

//console.log('options:', options);

// Create request to get data
request(options, (err, response, body) => {
    if (err) {
        //console.log(err);
    } else {
        console.log('body:', body);
    }
});
VnDevil
  • 1,321
  • 15
  • 13