简体   繁体   English

Azure Runbook-解析Python参数

[英]Azure Runbooks - Parse Python Params

I have a runbook and I am trying to basically parse parameters I entering into the body of the POST request (using Postman). 我有一个运行手册,我试图基本上解析我输入到POST请求正文中的参数(使用Postman)。 I looked at this thread , but couldn't get it to work. 我看着这个线程 ,但是无法正常工作。

My runbook's code where I am trying to get the params: 我尝试获取参数的Runbook代码:

mode = str(sys.argv[1])
resource_group_name = str(sys.argv[2])
vm_name = str(sys.argv[3])

here's my Postman call: 这是我的邮递员电话: 邮差

Error message: 错误信息:

in raw_decode obj, end = self.scan_once(s, idx)ValueError: Expecting property name: line 1 column 2 (char 1) 在raw_decode obj中,end = self.scan_once(s,idx)ValueError:预期属性名称:第1行第2列(字符1)

This is because when you pass the json from postman to runbook, the runbook will take the whole json string as one parameter, you can use print(sys.argv[1]) to check this behavior. 这是因为当您将json从邮递员传递到runbook时,runbook将把整个json字符串作为一个参数,您可以使用print(sys.argv[1])来检查此行为。 The output like below: 输出如下:

在此处输入图片说明

In your case, there is a workaround. 在您的情况下,有一种解决方法。 When you get the input parameter, get this section after RequestBody: , this one: {"resource_group_name":"vv1","vm_name":"vv2"},which is a json string Then you can parse the json string, get the value you want. 当您获得输入参数时,请在RequestBody:之后获得此部分:{“ resource_group_name”:“ vv1”,“ vm_name”:“ vv2”},这是一个json字符串然后您可以解析json字符串,获取想要的价值。

Sample code as below: 示例代码如下:

import sys
import json

#view the input parameter
print(sys.argv[1])

input_str = sys.argv[1]

# use "1" in index() method, to ignore the first { symbol in the input parameter
start_str = input_str.index("{",1)

end_str = input_str.index("}",1)

str = input_str[start_str:end_str+1]

text = json.loads(str)#parse the json string

#check the value
print("resource_group_name: "+text["resource_group_name"])
print("vm_name: "+text["vm_name"])

Test result as below: 测试结果如下:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM