javascript - REST adapter to work with https -


i writing rest adapter in express translates incoming rest calls rest api exposed northbound device. essentially, client device sending request rest api exposed adapter , adapter sends rest request device. adapter both rest server client devices rest client northbound device.

var express = require('express'); var router = express.router(); var request = require('request');  router.route('/')     .get(function(req, res) {                    request('url'', function (error, response, body) {              if (!error && response.statuscode == 200) {                 console.log("sent mos");                 console.log(body) // print google web page.             }         })     });  module.exports = router; 

i'm using request module. northbound device exposing https rest api. when insert https (mind 's') rest api call in url section in code above don't answer northbound device.

it works if 'url' above pointing https website, https://www.httpsnow.org/. also, when use https rest api in postman or browser on same device, proper response.

so don't understand why work postman, not code. broser had accept certificate first guess issue code not authenticated, how build authentication in?

it's trivial error - server opens connection https server, server's certificate appears invalid, handshake refused , error instead of response.

luckily, request supports custom https certificates(i.e. certificate shown invalid show valid) natively.

you can use agentoptions option this. download certificate had accept browser(they should provide 1 in docs) , save ca.cert.pem(or other extension, depending on provide) in same folder node.js app you're building.

then use this:

var request = require('request'); var fs = require('fs');  request({     url: 'https://api.some-server.com/',     agentoptions: {         ca: fs.readfilesync('ca.cert.pem') //note: load ca synchronously. avoid doing in production.     } }, function(error, response, body){   if(error){     console.warn(error); //if error occurred, you'll informed     return; //won't run rest of function   }    if(response.statuscode === 200){     //do whatever want   } }); 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -