Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the difference between node.js https request and XMLHttpRequest?
    primarykey
    data
    text
    <p>What is the difference between a HTTPS request sent via node.js module and via XMLHttpRequest?</p> <p>I am trying to send a HTTPS GET request to amazon aws to get a security token from javascript (XMLHttpRequest) and it always fails with "Origin <a href="http://my_ip" rel="nofollow">http://my_ip</a> is not allowed by Access-Control-Allow-Origin", but if I send same HTTPS GET request via a node.js module it works fine.</p> <p>I am confused of this because if server does support CORS, any request from any where should fail, but it goes through via node.js, but not via XMLHttpRequest. </p> <p><strong>This FAILS</strong></p> <pre><code>var url_ = "https://sts.amazonaws.com/?Action=GetSessionToken" + "&amp;DurationSeconds=3600" + "&amp;AWSAccessKeyId=XXXXXXXXXXXXXXX" + "&amp;Version=2011-06-15" + "&amp;Timestamp=" + encode(timestamp) + "&amp;Signature=" + encode(hash) + "&amp;SignatureVersion=2&amp;SignatureMethod=HmacSHA256"; // Simple GET request $.get(url_, function(data) { alert("response: " + data); }); </code></pre> <p><strong>This WORKS</strong></p> <pre><code>var https = require('https'); var options = { host : 'sts.amazonaws.com', method : 'GET', path : '/?Action=GetSessionToken' + '&amp;DurationSeconds=3600' + '&amp;AWSAccessKeyId=XXXXXXXXXXXXXX' + '&amp;Version=2011-06-15' + '&amp;' + timestamp + '&amp;' + signature + '&amp;SignatureVersion=2&amp;SignatureMethod=HmacSHA256' }; https.get(options, function(res) { res.on('data', function(d) { process.stdout.write(d); }); }).on('error', function(e) { console.error(e); }); </code></pre> <p>Can anyone explain me how this works?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload