Azure自动化:使用python Runbook解析WEBHOOKDATA [英] Azure Automation: Parse WEBHOOKDATA with a python runbook
问题描述
在azure自动化中,当您为Runbook定义Webhook时,请求的标头将通过 WEBHOOKDATA
输入参数传递给Runbook.对于python工作簿,参数作为第一个参数传递给脚本.
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.
例如,这是一本Runbook:
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)
这是输入参数 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"}}
输出(基本上是 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
失败
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)
JSON语法对我来说是正确的.可能与编码或Azure自动化如何将参数传递给Runbook有关.
The JSON syntax looks correct to me. Might be something related to encoding, or how Azure automation pass the parameters to runbook.
推荐答案
实际上,我认为您的问题是格式错误的JSON字符串.请注意,您的字符串没有引号:
actually I think that your problem is a malformed JSON string. Notice that your string does not have quotes:
{WebhookName:python-Test-Arguments}
应该是这样的:
{"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.
这篇关于Azure自动化:使用python Runbook解析WEBHOOKDATA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!