Solved

Curl format for Alert Webhook


Userlevel 3
Badge +3

Has anyone done this, or knows how to help?

 

I’m trying to create an alert from a windows script using curl.

I have created an alert with a simple expected payload 

{
  "Source": "$SOURCE",
  "Status": "$STATUS"
}
 

What format should I use? I’ve tried different variations on the below, and pretty sure I get a connection, but I’m not passing the data correctly (no Alert is generated).

curl.exe -H "Content-Type:application/json" -d "{'Source':'myscript', 'Status':'OK' }" https://mycompany.alerts.freshservice.com/integrations/alertid/alerts?auth-key=myauthkey

(mycompanyalertidmyauthkey removed from the above, but I have these values from the webhook setup in Freshservice)

 

Regasds,

Amdrew

 

icon

Best answer by aread007 18 July 2022, 08:47

View original

11 replies

Userlevel 3
Badge +3

I have got to the point of the alert being created, but the data is not passed.

curl.exe -X POST "https://mycompany.alerts.freshservice.com/integrations/alertid/alerts?auth-key=myauthkey" -H "Content-Type: application/json" -d "{'Source':'myscript', 'Status':'OK'}"

 

The above gives the error

curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL

If I remove one of the fields (Source or Status) I only get on line of the errors (curl: (3) URL using bad...)

This leads me to believe the issue is with the format of the data being passed.

Anyone have any ideas on this?

Regards,

Andrew

 

 

 

Userlevel 7
Badge +11

Hello  @aread007

 

I haven’t seen a  API  for alert. Standard API for Freshservice is https://api.freshservice.com/v2/

The key is used in another way., 

Userlevel 3
Badge +3

Not sure if anyone is on this journey with me….

 

This works on Linux:

curl -X POST 'https://mycompany.alerts.freshservice.com/integrations/alertD/alerts?auth-key=myauthkey -H 'Content-Type:application/json' -d '{"Source":"sourcesystem", "Status":"Ok"}'

I’ve tried all sorts of combo’s of single and double quotes on Windows (maybe that is the issue???) but get these errors

curl: (3) URL using bad/illegal format or missing URL
curl: (3) unmatched close brace/bracket in URL position 10:

Userlevel 7
Badge +11

Where did you find the API documents for this ? 

Userlevel 3
Badge +3

OK, the journey is over.

It was the quotes causing issues on Windows machine. I used a backslash to escape the quotes, and now working on Windows.

Hopefully this helps someone else in the future:

curl -X POST -H "Content-type:application/json" -d "{\"Source\": \"sourcesystem\", \"Status\":\"Ok\"}" https://mycompany.alerts.freshservice.com/integrations/alertid/alerts?auth-key=myauthkey

Userlevel 7
Badge +11

Do you use powershell? Then you can use the Invoke-RestMethod (Microsoft.PowerShell.Utility) - PowerShell | Microsoft Docs command. 

Userlevel 3
Badge +3

Where did you find the API documents for this ? 

I didn’t find any documentation. It was trial an error and google searching.

It would have gone a lot quicker if I had started on Linux, confirmed the Webhook worked, and then figured out the weird windows syntax.

Userlevel 3
Badge +3

Do you use powershell? Then you can use the Invoke-RestMethod (Microsoft.PowerShell.Utility) - PowerShell | Microsoft Docs command. 

The existing scripts are not powershell, but future ones may be. Thanks for the suggestion.

Userlevel 7
Badge +14

@aread007 This is so cool, can you show us the end result? What was the usecase behind this? I wonder if we should let the Freshservice API Product team know so that they can add it to the documentation! Great work!!! 

Userlevel 3
Badge +3

@aread007 This is so cool, can you show us the end result? What was the usecase behind this? I wonder if we should let the Freshservice API Product team know so that they can add it to the documentation! Great work!!! 

Thanks Zach,

First, this was mainly trial and error. This was a proof of concept. I hope this enough for myself (and others now) to extend on. 

My notes below are a little rushed, but hopefully fill in some gaps.

 

  1. In the Alerts Module, I selected Add monitoring tool.
  2. I selected Webhook (as opposed to one of the preconfigured integrations)
  3. For Configure alert payload I created a very simple structure: 

{
  "Source": "$SOURCE",
  "Status": "$STATUS"
}

  1. I then mapped the $SOURCE to the Subject and Resource fields in the Alert config (See screenshot at the end of this post)

To raise alert , I just call this command in a script:

curl -X POST -H "Content-type:application/json" -d "{\"Source\": \"Example Source Platform\", \"Status\":\"Down\"}" https://company.alerts.freshservice.com/integrations/alertid/alerts?auth-key=apikey

[bold items have been removed for security reasons]

 

IMAGE 1: Alert Raised

 

To Clear the alert, this command works:

curl -X POST -H "Content-type:application/json" -d "{\"Source\": \"Example Source Platform\", \"Status\":\"Up\"}" https://company.alerts.freshservice.com/integrations/alertid/alerts?auth-key=apikey

 

IMAGE 2: Alert Cleared

 

So, my use case was a script that looks for a file in a directory, and if it exists, FTP’s the file to another location. The script runs every 10 minutes. The file transfer is not time critical, as long as it happens within 24 hours, it is fine. On error, the script branches to the raise alert. If after a number of retries the transfer is successful, the clear alert curl is run. This sets the Alert to Clear. If an incident had been raised, it will also close the incident related to the Alert (based on the Alert Rules configuration).

 

The Alert Rules in Freshservice let you decide under what conditions to raise an incident for an alert. I haven't had a good look, but it appears that Workflow Automator does not deal with Alerts, so hopefully the Alert Rules is flexible enough to deal with most situations.

 

Other Points:

I tried passing the auth key as a header, but couldn’t get this working. I don't think including the auth key in the URL is good practice. I have now seen some examples if the general API documentation on how to pass this auth key outside of the URL, I just haven’t tested it yet.

My Proof of Concept only passed two items. It would be better to pass more information to an alert, and use variables in the curl command to pass these. e.g. error message, a user friendly description….

 

​​​​​​​IMAGE 3: Webhook Config

 

Regards,

Andrew

Userlevel 3
Badge +3

Maybe my last post.

Here is how to move the apikey out of the URL and into a header:

curl -X POST -H " Authorization: auth-key apikey  " - -H "Content-type:application/json" -d "{\"Source\": \"Example Source Platform\", \"Status\":\"Up\"}" https://company.alerts.freshservice.com/integrations/alertid/alerts

 

Reply