问题
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.
回答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.
来源:https://stackoverflow.com/questions/49402149/azure-automation-parse-webhookdata-with-a-python-runbook