{"id":2760,"date":"2016-11-14T10:16:42","date_gmt":"2016-11-14T10:16:42","guid":{"rendered":"https:\/\/www.mjtnet.com\/blog\/?p=2760"},"modified":"2016-11-14T10:16:42","modified_gmt":"2016-11-14T10:16:42","slug":"http-request-with-custom-headers-using-python","status":"publish","type":"post","link":"https:\/\/www.mjtnet.com\/blog\/2016\/11\/14\/http-request-with-custom-headers-using-python\/","title":{"rendered":"HTTP Request With Custom Headers using Python"},"content":{"rendered":"<p>As you will hopefully already know by now we added the ability to use Python code a while back in <a href=\"https:\/\/www.mjtnet.com\/macro-scheduler.htm\">Macro Scheduler 14.2<\/a>.<\/p>\n<p>A customer recently had a need to retrieve some data from a web service which requires some custom authentication headers to be sent with the request.<\/p>\n<p>It&#8217;s really easy to do this using Python.  Here&#8217;s a made-up example:<\/p>\n<pre name=\"code\" class=\"macroscript\">\/*\r\npython_code:\r\n \r\nimport urllib2\r\n\r\ncustom_headers={\"X-Custom-Application-Id\" : \"4020c4b37ead0a834c0010a9\",\r\n                \"X-Custom-REST-API-Key\": \"49eade9c-d70a-4d3d-b552-4bd9f05966fc\"}\r\nurl=\"https:\/\/api.custom.com\/v1\/objects\/object_1\/things\"\r\n\r\nrequest = urllib2.Request(url, headers=custom_headers)\r\ncontents = urllib2.urlopen(request).read()\r\n*\/\r\n \r\n\/\/Load the Python code to a variable\r\nLabelToVar>python_code,pcode\r\n \r\n\/\/Run the code and request the value of the contents variable ...\r\nPYExec>pcode,output,contents\r\n\r\n\/\/content of request is now in \"contents\" variable.<\/pre>\n<p>For more on Python in Macro Scheduler see <a href=\"https:\/\/www.mjtnet.com\/blog\/2014\/11\/07\/macro-scheduler-14-2-python-json-xml-auto-documenting-macro-recorder\/\">here<\/a> and <a href=\"https:\/\/www.mjtnet.com\/manual\/pyexec.htm\">here<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>As you will hopefully already know by now we added the ability to use Python code a while back in Macro Scheduler 14.2. A customer recently had a need to retrieve some data from a web service which requires some custom authentication headers to be sent with the request. It&#8217;s really easy to do this [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/2760"}],"collection":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/comments?post=2760"}],"version-history":[{"count":4,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/2760\/revisions"}],"predecessor-version":[{"id":2764,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/2760\/revisions\/2764"}],"wp:attachment":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/media?parent=2760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/categories?post=2760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/tags?post=2760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}