1

In azure automation, when you define a webhook for a runbook the headers of the request are passed to the runbook via the WEBHOOKDATA input parameter. For python workbooks, the parameter is passed as first argument to the script.

For example, here is a runbook:

import json
import sys

print(sys.argv)

if len(sys.argv) > 1 :
    test = json.loads(sys.argv[1])
    print(test)

Here is the input parameter WEBHOOKDATA

{"WebhookName":"python-Test-Arguments","RequestBody":"","RequestHeader":{"Cache-Control":"no-cache","Connection":"keep-alive","Accept":"*/*","Accept-Encoding":"gzip","Host":"s2events.azure-automation.net","User-Agent":"PostmanRuntime/7.1.1","action":"myaction","Postman-Token":"312ce179-d2d1-4b5d-935c-d801fc0ba114","x-ms-request-id":"e6b3a5e3-17b3-4d2a-a00c-a1be928acca2"}}

The output (basically print(sys.argv))

['C:\\Temp\\yjzgss3j.git\\caf4e30b-0cb1-4c60-9e93-e2315b376634', '{WebhookName:python-Test-Arguments,RequestBody:",RequestHeader:{Cache-Control:no-cache,Connection:keep-alive,Accept:*/*,Accept-Encoding:gzip,Host:s2events.azure-automation.net,User-Agent:PostmanRuntime/7.1.1,action:myaction,Postman-Token:312ce179-d2d1-4b5d-935c-d801fc0ba114,x-ms-request-id:e6b3a5e3-17b3-4d2a-a00c-a1be928acca2}}']

json.loads fails

Traceback (most recent call last): File "C:\Temp\yjzgss3j.git\caf4e30b-0cb1-4c60-9e93-e2315b376634",
line 7, in <module> test = json.loads(sys.argv[1].strip()) File "C:\Python27\lib\json\__init__.py",
line 339, in loads return _default_decoder.decode(s) File "C:\Python27\lib\json\decoder.py",
line 364, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Python27\lib\json\decoder.py",
line 380, in raw_decode obj, end = self.scan_once(s, idx)ValueError: Expecting property name: line 1 column 2 (char 1)

The JSON syntax looks correct to me. Might be something related to encoding, or how Azure automation pass the parameters to runbook.

2 Answers 2

1

actually I think that your problem is a malformed JSON string. Notice that your string does not have quotes:

{WebhookName:python-Test-Arguments}

It should be something like this:

{"WebhookName":"python-Test-Arguments"}

To solve this you need to fix your response string. This is a thread that talk about this kind of operation.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I used some regex to bring quotes back. And made an example on repl.it
1
import json
import ast


if __name__ == "__main__":
    webhook_data = sys.argv[1]
    parsed_data = data = ast.literal_eval(webhook_data)
    WebhookName=parsed_data['WebhookName']
    RequestBody=json.loads(parsed_data['RequestBody'])
    RequestHeader=parsed_data['RequestHeader']
    print(WebhookName)
    print(RequestBody)
    print(RequestHeader)

This totally works for parsing of webhook data in automation runbooks with python3. I have tested with eventgrid subscribed messages.

This is how my received webhook data (formatted and anonymised) looks like:

{
  "WebhookName": "webhook-for-kv-secret-expired",
  "RequestBody": "[{\"id\":\"<uuid>\",\"topic\":\"/subscriptions/<subscription-id>/resourceGroups/<resource-group>/providers/Microsoft.KeyVault/vaults/<vault-name>\",\"subject\":\"<subject>\",\"eventType\":\"Microsoft.KeyVault.SecretNewVersionCreated\",\"data\":{\"Id\":\"https://<vault-name>.vault.azure.net/secrets/<secret-name>/<secret-version>\",\"VaultName\":\"<vault-name>\",\"ObjectType\":\"Secret\",\"ObjectName\":\"<secret-name>\",\"Version\":\"<secret-version>\",\"NBF\":null,\"EXP\":null},\"dataVersion\":\"1\",\"metadataVersion\":\"1\",\"eventTime\":\"2025-05-28T14:20:04.0885806Z\"}]",
  "RequestHeader": {
    "aeg-subscription-name": "EVENT-GRID-SYSTEM-SUBSCRIPTION-SECRET-EXPIRED",
    "aeg-delivery-count": "0",
    "aeg-metadata-version": "1",
    "aeg-event-type": "Notification",
    "aeg-data-version": "1",
    "Connection": "Keep-Alive",
    "Host": "<host>.webhook.noe.azure-automation.net",
    "x-ms-request-id": "<request-id>"
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.