main.js 1.1 KB

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