title: NodeJS equivalent of a curl request
body: <p>I am trying to convert a curl request to a nodeJS script, but somehow it fails on me., by failing means, that the url I setup with cloudflare triggers a captcha whilst it doesn't when I use the curl request I copied from dev tools > network tab.</p>
  
  <p>here is the curl</p>
  
  <pre><code>curl -q 'https://foo.bar/path' -H 'cookie: somecookie' -H 'origin: https://foo.bar' -H 'accept-encoding: gzip, deflate, br' -H 'accept-language: en-US,en;q=0.9,pt;q=0.8' -H 'user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36' -H 'content-type: application/x-www-form-urlencoded; charset=UTF-8' -H 'accept: */*' -H 'referer: https://foo.bar/path' -H 'authority: www.foo.bar' -H 'x-requested-with: XMLHttpRequest' --data "foo=bazz" --compressed
  </code></pre>
  
  <p>and here is the nodeJS code</p>
  
  <pre><code>var request = require('request');
  var url = 'https://foo.bar/path';
  var cookie = 'somecookie';
  var ua = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36';
  
  var baseRequest = request.defaults({
    headers: {
      'Cookie': cookie,
      'User-Agent': ua,
      'origin': 'https://foo.bar',
      'referer': 'https://foo.bar/path'
    }
  });
  
  baseRequest.post({ url: url, formData: {
    foo: 'bazz'
  }}, function(err, response, body) {
    console.log(body);
  });
  </code></pre>