title: Adding a curl request into a serviceNow business rule (javascript)
body: <p><strong>IN SHORT:</strong>
  How do I write a javascript code that can send an API PUT message to my flask server?</p>
  
  <p><strong>CONTEXT</strong></p>
  
  <p>I have a simple flask applications like as described here: <a href="http://flask-restful.readthedocs.io/en/0.3.5/quickstart.html" rel="nofollow noreferrer">http://flask-restful.readthedocs.io/en/0.3.5/quickstart.html</a></p>
  
  <p>But I have added CORS capability by adding this to the top: </p>
  
  <pre><code>from flask import Flask
  from flask_restful import reqparse, abort, Api, Resource
  from flask_cors import CORS
  
  
  app = Flask(__name__)
  CORS(app)
  api = Api(app)
  </code></pre>
  
  <p>It is on a server at <code>[server_ip]</code></p>
  
  <p>There is a ticketing service applications called serviceNOW and they allow one to run simple javascript commands in their business rules.</p>
  
  <p>They supply the context:</p>
  
  <pre><code>(function executeRule(current, previous /*null when async*/) {
  
  
  
  })(current, previous);
  </code></pre>
  
  <p>And I want to add the functionality: <code>curl http://[server_ip]:5000/todos/todo3 -d "task=something different" -X PUT -v</code></p>
  
  <p><strong>MAIN IDEA:</strong> I want to send API calls from the serviceNOW server to my flask server when a business rule is called</p>
  
  <p>Using the chrome console while on the serviceNOW server page I have run the following code:</p>
  
  <pre><code>var xhttp = new XMLHttpRequest();
  xhttp.open("GET", "http://[my_server]:5000/todos/todo3", false);
  xhttp.send();
  </code></pre>
  
  <p>And i get the error:</p>
  
  <pre><code>Mixed Content: The page at 'https://[serviceNOW instance]' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://[server_ip]:5000/todos/todo3'. This request has been blocked; the content must be served over HTTPS.
  (anonymous) @ VM1629:1
  VM1629:1 Uncaught DOMException: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://[server_ip]:5000/todos/todo3'.
      at &lt;anonymous&gt;:1:7
  </code></pre>
  
  <p><strong>EDIT 1</strong></p>
  
  <p>I changed:</p>
  
  <pre><code>xhttp.open("GET", "http://[my_server]:5000/todos/todo3", false);
  xhttp.send();
  </code></pre>
  
  <p>to</p>
  
  <pre><code>xhttp.open("PUT", "https://[my_server]:5000/todos/todo3", false);`
  xhttp.send(JSON.stringify({ "task": "new task2"}));
  </code></pre>
  
  <p>and i get: </p>
  
  <pre><code>VM1698:1 OPTIONS https://[server_ip]:5000/todos/todo5 
  (anonymous) @ VM1698:1
  VM1698:1 Uncaught DOMException: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'https://[server_ip]:5000/todos/todo5'.
      at &lt;anonymous&gt;:1:7
  </code></pre>
  
  <p>My server says: </p>
  
  <pre><code>code 400, message Bad HTTP/0.9 request type ('\x16\x03\x...more hex..')
  ?m?~)]?n??K...more stuf..?,?0̨̩????/5" 400 -
  </code></pre>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>This is my current code:</p>
  
  <pre><code>var xhttp = new XMLHttpRequest();    
  xhttp.open("PUT", "https://[my_server]:5000/todos/todo3", false);`
  xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
  xhttp.send(JSON.stringify({ "task": "new task2"}));
  </code></pre>
  
  <p>This is my current console side error:</p>
  
  <pre><code>DOMException: Failed to execute 'send' on 'XMLHttpRequest': Failed to load
  </code></pre>
  
  <p>This is my current server side error:</p>
  
  <pre><code>code 400, message Bad HTTP/0.9 request type
  </code></pre>
  
  <p>This curl works: </p>
  
  <pre><code>curl http://[server_ip]:5000/todos/todo5 -d "task=something differentyea" -X PUT -v
  </code></pre>
  
  <p><strong>EDIT 3</strong></p>
  
  <p>This is my current code:</p>
  
  <pre><code>var xhttp = new XMLHttpRequest();    
  xhttp.open("PUT", "https://[my_server]:5000/todos/todo3", true);`
  xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
  xhttp.send(JSON.stringify({ "task": "new task2"}));
  </code></pre>
  
  <p>This is my current error:</p>
  
  <pre><code>OPTIONS https://[server_ip:5000/todos/todo6 net::ERR_SSL_PROTOCOL_ERROR
  </code></pre>
  
  <p><strong>EDIT 4</strong></p>
  
  <p>This code works (note the http and not https):</p>
  
  <pre><code>var xhttp = new XMLHttpRequest();    
  xhttp.open("PUT", "http://[my_server]:5000/todos/todo3", true);`
  xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
  xhttp.send(JSON.stringify({ "task": "new task2"}));
  </code></pre>
  
  <p>But I have to run it from a none https site.</p>