2

Im using NodeJS Request - Simplified HTTP Client

I seem to have problem working with HTTPS website, I am not getting result.

var request = require('request');

request.post({
    url: "",//your url
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    },

    form: {
        myfield: "myfieldvalue"
    }
},function (response, err, body){
    console.log('Body:',JSON.parse(body));
}.bind(this));

Tested the API endpoint(which I cant share) on Postman, I just turn off SSL and it works, how do I do the same with request plugin?

1 Answers1

8

Just add this lines:

    rejectUnauthorized: false,//add when working with https sites
    requestCert: false,//add when working with https sites
    agent: false,//add when working with https sites

So your code would look like this:

var request = require('request');

request.post({
    url: "",//your url
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    },
    rejectUnauthorized: false,//add when working with https sites
    requestCert: false,//add when working with https sites
    agent: false,//add when working with https sites
    form: {
        myfield: "myfieldvalue"
    }
},function (response, err, body){
    console.log('Body:',JSON.parse(body));
}.bind(this));
Mark Ryan Orosa
  • 847
  • 1
  • 6
  • 21
  • Could you please clarify what effect your suggested settings will cause? Options rejectUnauthorized and requestCert are not mentioned in the docs. – vsenko Oct 27 '17 at 09:26
  • 1
    @vsenko, essentially it just disable the ssl verification. which is a bit unsafe, if you dont trust the service you are connecting to. – Mark Ryan Orosa Oct 27 '17 at 11:36
  • 5
    It seems to me that it would be a good idea to clarify this in the answer, so that no one would ever use this approach in production. – vsenko Oct 27 '17 at 14:30
  • @MarkRyanOrosa which one actually disables ssl verification? rejectUnauthorized? requestCert? agent? And what do the other two do? – Frikster Jan 14 '22 at 22:40