0

I have a UI Automation script, written in JavaScript. From that script, how do I execute a GET request to a URL with a cookie? E.g:

curl "https://example.com/path?a=123&b=cool" --cookie "c=12"

Or, if you know how to run system commands from JavaScript, like you can do in Ruby, that'll work too.

ma11hew28
  • 121,420
  • 116
  • 450
  • 651
  • For those who do not know the curl command-line options by heart, an explanation what your curl call actually does would be nice. ;) – Tomalak Jun 09 '11 at 19:23
  • `man curl`. Then, type `/` to search. Then, type `-b` and hit enter. You will see: `-b/--cookie (HTTP) Pass the data to the HTTP server as a cookie`. I updated the question to make it more clear. – ma11hew28 Jun 13 '11 at 00:16

2 Answers2

1

See this thread on using jQuery to set cookies in the browser.

Community
  • 1
  • 1
hross
  • 3,633
  • 2
  • 27
  • 31
  • I'm sorry, I'm not sure how that thread is relevant. I'm asking how to send a GET request with a cookie to a URL with JavaScript. And, the JavaScript is being run by [Mac Developer Tools : Instruments](http://developer.apple.com/technologies/tools/), not by a browser. – ma11hew28 Jun 13 '11 at 00:26
1

After checking out HTTP GET request in JavaScript? and reading step 7 of "3.6.2. The setRequestHeader() method" in the W3C specs for XMLHttpRequest, I came up with:

function httpGet(url, opts={}) {
  var client = new XMLHttpRequest();
  client.open('GET', url, false); // not async
  if (opts.cookie) {
    client.setRequestHeader('Cookie', opts.cookie);
  }
  client.send();
}

FYI, I'm disregarding the response because I'm deleting a resource (perhaps I should verify the correct response so that I know the resource was deleted). So, yes, this should be a DELETE request, but they configured the server to accept a GET request, and I have no control over this.

Community
  • 1
  • 1
ma11hew28
  • 121,420
  • 116
  • 450
  • 651