NodeJS Request Post not working for HTTPS website
Asked Answered
O

1

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?

Offertory answered 27/10, 2017 at 6:40 Comment(6)
What's the error...Racklin
Why loopbackjs tag?Frontpage
There is not clearly shaped question and description, which is very important for the stackoverflow and the users which will check after for similar questions.Venterea
I think this one relates to origin when do server-call-server.Fusillade
@Frontpage I am using loopback with this one.Offertory
Q do you used this by docs? github.com/request/request#tlsssl-protocolStoddard
F
9

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));
Footstone answered 27/10, 2017 at 6:43 Comment(4)
Could you please clarify what effect your suggested settings will cause? Options rejectUnauthorized and requestCert are not mentioned in the docs.Frontpage
@vsenko, essentially it just disable the ssl verification. which is a bit unsafe, if you dont trust the service you are connecting to.Footstone
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.Frontpage
@MarkRyanOrosa which one actually disables ssl verification? rejectUnauthorized? requestCert? agent? And what do the other two do?Bristletail

© 2022 - 2024 — McMap. All rights reserved.