main.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. // Get the web subsystem.
  2. var web = Atomic.getWeb();
  3. // Non-blocking http request runs asynchronously.
  4. var request = web.makeWebRequest("GET", "https://httpbin.org/get");
  5. // Add some request headers.
  6. request.setRequestHeader("Some-Special-Header", "Special header value");
  7. request.setRequestHeader("A-Magic-Header", "Magic header value");
  8. // Listen for the "complete" event to see when the response is complete.
  9. request.subscribeToEvent("complete", function (event) {
  10. if (event.error) {
  11. // When something goes wrong, print the error, then return.
  12. console.log("Error:\n" + event.error);
  13. return;
  14. }
  15. // We're done, so print the response headers, then the content.
  16. // The getAllResponseHeaders() function gives a single string will all response headers.
  17. console.log("Headers:\n" + request.getAllResponseHeaders());
  18. // The "httpbin.org/get" call will show request headers in the response content.
  19. console.log("Content:\n" + event.download.readString());
  20. });
  21. // Nothing happens until send() is called.
  22. console.log("Sending . . .\n");
  23. request.send();