What is the difference between an HTTPS request sent through the node.js module and via XMLHttpRequest?
I am trying to send an HTTPS GET request to amazon aws to get the security token from javascript (XMLHttpRequest) and it always fails with "Origin http: // my_ip is not allowed Access-Control-Allow-Origin", but if I send one and the same HTTPS GET request through the node.js module, it works fine.
I got confused about this because if the server supports CORS, any request from anywhere should fail, but it goes through node.js, but not through XMLHttpRequest.
This FAILS
var url_ = "https://sts.amazonaws.com/?Action=GetSessionToken" + "&DurationSeconds=3600" + "&AWSAccessKeyId=XXXXXXXXXXXXXXX" + "&Version=2011-06-15" + "&Timestamp=" + encode(timestamp) + "&Signature=" + encode(hash) + "&SignatureVersion=2&SignatureMethod=HmacSHA256"; // Simple GET request $.get(url_, function(data) { alert("response: " + data); });
This WORKS
var https = require('https'); var options = { host : 'sts.amazonaws.com', method : 'GET', path : '/?Action=GetSessionToken' + '&DurationSeconds=3600' + '&AWSAccessKeyId=XXXXXXXXXXXXXX' + '&Version=2011-06-15' + '&' + timestamp + '&' + signature + '&SignatureVersion=2&SignatureMethod=HmacSHA256' }; https.get(options, function(res) { res.on('data', function(d) { process.stdout.write(d); }); }).on('error', function(e) { console.error(e); });
Can someone explain to me how this works?
source share