Since a webhook is technically just an HTTP POST message, we can use PowerShell and its Invoke-WebRequest cmdlet to craft an HTTP message to a service that supports a webhook URL.

In today's IT world, it seems like everything has an API. It's now good for business and cool to be a company that's open, transparent, and allows third-party integrations into their service. Providing an API to that service allows developers to interact with your product via code instead of via a web interface.

One useful feature of an HTTP API is a webhook. In a nutshell, a webhook is a web event. It's technically an HTTP POST event that sends information to a certain URL when some kind of action triggers it. For example, someone sending a tweet, committing new code to a GitHub repository, or adding a file to your Dropbox account may trigger the event. It generates the webhook automatically, which is simply part of the event itself.

An API using webhooks allows developers to "catch" these webhooks and invoke some kind of code as a result of that event. To catch these webhooks, a developer can write code that listens for them.

For example, I use a service called Zapier. This service is like the popular "IF This Then That" (IFTTT) service that allows you to perform actions on hundreds of different services from a trigger from some other kind of service. One of the actions that Zapier supports is a webhook. Zapier sets up a webhook URL and listens for any incoming HTTP POST messages to that URL. When it receives one, it then automatically kicks off some kind of other process.

Webhook example

Webhook example

When you've set up this "catch hook" trigger, it then begins listening for any HTTP POST messages sent to it matching a specific JSON body. To invoke this webhook, I first need to create the body of the HTTP request using JavaScript Object Notation (JSON). If you're working with PowerShell objects, this is where the ConvertTo-JSON and ConvertFrom-JSON cmdlets would come in handy.

For the Zapier example, the webhook is expecting a specific JSON string for which I've preconfigured my webhook. Here's an example of one that's looking for a client name, email subject, and an email address.

$httpBody = '{{"body": "email body", "Client": "Acme", "subject": "Some subject", "email_to": "joe@acme.com"}}'

Once I've created the HTTP body in the form that the webhook URL is looking for, I can then use Invoke-WebRequest to initiate an HTTP POST method to the webhook URL using the POST method and passing the JSON HTTP body inside of the request.

Subscribe to 4sysops newsletter!

$null = Invoke-WebRequest -Uri 'https://hooks.zapier.com/hooks/catch/XXXXX' ‑Method Post -Body $httpBody

Getting the JSON in the body structured correctly for the service you're calling is the hard part. Each service is going to be different. After this, however, the rest is easy. By using Invoke-WebRequest, we just point to the webhook URL and send the JSON via POST, which will trigger the webhook!

avatar
0 Comments

Leave a reply

Please enclose code in pre tags

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2021

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account