Monday, 5 June 2017

how to properly access AWS Api Gateway from Node Web application

I'm experiencing a wired issue, where the same exact code (which accesses AWS Api Gateway) runs just fine if submitted this way: node app.js, but produces 307 Redirect, when submitted from within Node/Express app.

Here is the "stand-alone" single file node program (app.js) :

var http = require("https");

var options = {
      "method": "POST",
        "hostname": "ipzjnsvxnd.execute-api.us-west-2.amazonaws.com",
          "port": null,
            "path": "/DEV/execution",
              "headers": {
                      "cache-control": "no-cache",
                          "postman-token": "b929e970-fe22-0f4f-e659-117890fda955"
                                }
};

var req = http.request(options, function (res) {
      var chunks = [];

        res.on("data", function (chunk) {
                chunks.push(chunk);
                  });

          res.on("end", function () {
                  var body = Buffer.concat(chunks);
                      console.log(body.toString());
                        });
});

req.write("{\n  \"input\": \"{ \\\"account_key\\\": \\\"9990\\\", \\\"acc1\\\": \\\"1235813\\\", \\\"acc2\\\": \\\"13711\\\",\\\"amount\\\": \\\"1000.00\\\", \\\"city\\\": \\\"BrandonTown\\\" }\",\n  \"name\": \"RequiredUniqueValueGoesHere19090\",\n  \"stateMachineArn\": \"arn:aws:states:us-west-2:217465658899:stateMachine:FICO_StateMachine3\"\n}");
req.end();

And here is the same code, as a part of a Node/Express Web app:

module.exports = function(app) {

    var querystring = require('querystring');
    var http = require('http');
    http.post = require('http-post');

    app.get('*', function(req, res) {
        res.sendfile('./public/index.html');
    });

    app.post("/customerinfo", function(req, res) {

        var options = {
            "method": "POST",
            "hostname": "ipzjnsvxnd.execute-api.us-west-2.amazonaws.com",
            "path": "/DEV/execution",
            "headers": {
                "cache-control": "no-cache",
                "postman-token": "b929e970-fe22-0f4f-e659-117890fda955"
            }
        };

          var req1 = http.request(options, function (res1) {
            var chunks = [];

            res1.on("data", function (chunk) {
                chunks.push(chunk);
            });

            res1.on("end", function () {
                var body = Buffer.concat(chunks);
                console.log(body.toString());
            });
        });

        req1.write("{\n  \"input\": \"{ \\\"account_key\\\": \\\"9990\\\", \\\"acc1\\\": \\\"1235813\\\", \\\"acc2\\\": \\\"13711\\\",\\\"amount\\\": \\\"1000.00\\\", \\\"city\\\": \\\"BrandonTown\\\" }\",\n  \"name\": \"RequiredUniqueValueGoesHere1239091\",\n  \"stateMachineArn\": \"arn:aws:states:us-west-2:217465658899:stateMachine:FICO_StateMachine3\"\n}");
        req1.end();
    });

};

The first one works fine when submitted: node app.js The second one returns:

<html>
<head><title>307 Temporary Redirect</title></head>
<body bgcolor="white">
<center><h1>307 Temporary Redirect</h1></center>
<hr><center>CloudFront</center>
</body>
</html>

<html>
<head><title>307 Temporary Redirect</title></head>
<body bgcolor="white">
<center><h1>307 Temporary Redirect</h1></center>
<hr><center>CloudFront</center>
</body>
</html>

<html>
<head><title>307 Temporary Redirect</title></head>



via Eugene Goldberg

No comments:

Post a Comment