JSON Web POST with Python

In an earlier post, it was outlined how to setup a Microsoft Flow workflow to generate a notification on a phone when a web request was made. This post will outline how to create such a web request using the popular scripting language Python.

Install Python

  • Ensure that Python (version 3) is installed on the system
  • During this process it is important to ensure that Python is added to the system PATH variable
    • This allows Python to be called from the command line
    • Having this ability will make it significantly easier to schedule this script to run when events occur, which is likely to be the role of the script.
  • After python is installed, we must install the “requests” library
    • This is the library that we will use to perform the actual request
    • Note: It is possible to perform the POST request without the “requests” library, however I believe it to be the cleanest method.
    • To install “requests” open a command line and enter “pip install requests”
      • If you get an error running this command, it is likely that Python was not added correctly to the system’s PATH variable

The Code Itself

  • First we need to import the “requests” library we just installed into the Python script
    • This is done with the following code:
    • import requests
  • Next we need to define the URL to which the request will be made
    • This can be found in your workflow you created previously and will be unique to each Flow
    • URL = "https://prod-02.australiasoutheast.logic.azure.com:443/workflows/.......
  • Next we need to define the JSON that will be sent in the request to provide the parameters to our notification service
    • For my notification I only had two parameters, a source and a message
    • For the purpose of this example I have set the source parameter to be “Computer” and the message parameter to be “42”
    • Parameters = {"Source" : "Computer", "Message" : "42"}
  • Finally we perform the actual request from the server
    • For the request to execute, we need to provide it with the URL and parameters we defined earlier
    • Request = requests.post(URL, json = Parameters)
  • After running the code you should now get a notification on your phone
  • By changing the values contained in the parameters section you can change the contents of the messages to whatever you wish.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s