Send an outbound POST request using HTTPS on Openshift/Heroku

家住魔仙堡 提交于 2019-12-25 07:36:42

问题


It seems like all outbound requests (e.g. http.request(…), restify.post(…)) send to an https:// URL are being converted to http:// for some reason (OpenShift proxying?). How can I call an external API and make sure I hit the HTTPS URL instead of the HTTP one?

Here's a concrete example. This call to the StubHub API works perfectly when I run it from localhost, but as soon as I run it in an OpenShift or Heroku NodeJS container/dyno, I get forced to http://api.stubhub.com/ and accordingly get an error.

var express = require('express'),
    server  = express(),
    fetch   = require('node-fetch')
    qs      = require('qs');

// api
var api = express.Router();
server.use('/api/v1', api);

api.post('/login', function (req, res) {
    fetch('https://api.stubhub.com/login', {
        method: 'POST',
        headers: {
            Authorization: 'Basic <TOKEN>',
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        body: qs.stringify({
            grant_type: 'password',
            username: req.query.username,
            password: req.query.password,
            scope: 'PRODUCTION',
        }),
    })
        .then(function (response) {
            res.send(response.json());
        });
});

// server
server.listen(process.env.NODE_PORT || 3030, process.env.NODE_IP || 'localhost');

来源:https://stackoverflow.com/questions/36142296/send-an-outbound-post-request-using-https-on-openshift-heroku

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!