Loading...
 

Greg`s Tech blog

Using Powershell to post data to IFTTT WebHooks service

Monday 07 of August, 2017

IFTTT has many useful triggers and I like Webhooks because it can enable so many fun interactions.  My goal today is sending JSON key:value pairs to WebHooks from Powershell (my preferred scripting language and now available on Linux!).  


WebHooks will accept three named parameters vis JSON (also form data and url parameters) that can be referenced within the Action of your applet.  The paramaeters are named value1, 2 & 3. so the JSON should look like this: 


{
    "value1":  "Good Morning",
    "value3":  "That is all.",
    "value2":  "Greg"
}


PowerShell has two methods for posting this to a URL Invoke-WebRequest and Invoke-Restmethod.  The latter is apparently a wrapper of the former and return onthe the string output from the POST. Because of the possible error-checking needs, I'll focus on Invoke-WebRequest.  


Here is the code that made this work:


$BaseURL = "https://maker.ifttt.com/trigger/GMhit/with/key/enteryourkeyhere"


  1. Note: The key (last part of URL is user unique

  2. The Trigger here is GMhit and unique to me. You would declare your own in the IFTTT service

$body = @{ value1="Good Morning" value2="Greg" value3="That is all." }



  1. Either works. Webrequest return status code

  2. Invoke-RestMethod -URI $BaseURL -Body (ConvertTo-Json $body) -Method Post -ContentType application/json

Invoke-WebRequest -URI $BaseURL -Body (ConvertTo-Json $body) -Method Post -ContentType application/json


Notes:



  • Setting the ContentType to `application/json` is important here.  This call didn't work until this was set correctly.

  • The value names are fixed and cannot be customized.