简体   繁体   中英

Nodejs https.request and axios

I am having a very weird issue by using the Nodejs https module. What I was trying to do is, calling the 3rd party API for some service, the following is my code:

const https = require("https");

function request(accessId, secretKey, host, api, body, timeout=3000) {
    let bodyString = JSON.stringify(body);
    let time = Math.round(new Date().getTime()/1000).toString();

    // I have implemented the signBody function
    let sign = signBody(accessId, secretKey, time, bodyString);
    let header = {
       "Content-Type": "application/json",
       "AccessId": accessId,
       "TimeStamp": time,
       "Sign": sign,
    };
    let options = {
       method: 'POST',
       timeout: timeout,
       headers: header,
    }
    let url = new URL(api,host);
    https.request(url, options, (res) => {...});
}

They weird part is, if I'm running the function by node xxx.js to trigger the request("MY_ACCESS_ID", "MY_SECRET_KEY", "https://api.xxxx.com", "/service/api/v3", MY_BODY) function, it works as expected. However, this request(...) function is part of my webserver, and it is used by a API (I'm using express.js), such as:

// the myService implemented the request() function
let myService = require("./myService.js")
router.get("/myAPI", (req, res, next) => {
    
    request("MY_ACCESS_ID", "MY_SECRET_KEY", "https://api.xxxx.com", "/service/api/v3", MY_BODY)
})

it always shows the error: Error: connect ECONNREFUSED 127.0.0.1:443

I have no idea why the same code is behaving different. I thought it probably https.request issue. they I tried to use axios for the post request. The other weird things showed up. By using the exactly same header , https.request() returns success from the service provider and axios.post returns error message: Sign check error, please check the way to generate Sign .

This is so crazy....no idea of this issue. Any idea ?? BTW, I have solved the issue by implementing as:

const https = require("https");

function request(accessId, secretKey, host, api, body, timeout=3000) {
    let bodyString = JSON.stringify(body);
    let time = Math.round(new Date().getTime()/1000).toString();

    // I have implemented the signBody function
    let sign = signBody(accessId, secretKey, time, bodyString);
    let header = {
       "Content-Type": "application/json",
       "AccessId": accessId,
       "TimeStamp": time,
       "Sign": sign,
    };
    let options = {
         hostname: host,
         path: api,
       method: 'POST',
       timeout: timeout,
       headers: header,
    }
    https.request(options, (res) => {...});
}

But still no idea what is the difference.

I would check the final url constructed inside the https.request method. The non-working version makes a request to 127.0.0.1:443 , which wouldn't work since your localhost doesn't support https(only http) and 443 is usually for https.

See https://nodejs.org/api/https.html#https_https_request_url_options_callback for default port number.

Axios has different implementation inside the post() method and it may manipulate your sign string by url encoding it before sending to the 3rd-party API.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM