HTTP Request With Custom Headers using Python

Published on November 14, 2016 by Marcus Tettmar in Scripting

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’s really easy to do this using Python. Here’s a made-up example:

/*
python_code:
 
import urllib2

custom_headers={"X-Custom-Application-Id" : "4020c4b37ead0a834c0010a9",
                "X-Custom-REST-API-Key": "49eade9c-d70a-4d3d-b552-4bd9f05966fc"}
url="https://api.custom.com/v1/objects/object_1/things"

request = urllib2.Request(url, headers=custom_headers)
contents = urllib2.urlopen(request).read()
*/
 
//Load the Python code to a variable
LabelToVar>python_code,pcode
 
//Run the code and request the value of the contents variable ...
PYExec>pcode,output,contents

//content of request is now in "contents" variable.

For more on Python in Macro Scheduler see here and here.