0

I have a runbook and I am trying to basically parse parameters I entering into the body of the POST request (using 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:

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

here's my Postman call: Postman

Error message:

in raw_decode obj, end = self.scan_once(s, idx)ValueError: Expecting property name: line 1 column 2 (char 1)

Jay Gong
  • 23,163
  • 2
  • 27
  • 32
DevKing
  • 211
  • 5
  • 14

1 Answers1

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. The output like below:

enter image description here

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.

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:

enter image description here

Ivan Glasenberg
  • 29,865
  • 2
  • 44
  • 60
  • Hi, appreciate your feedback. But I am not sure how you got this to work. There's a few things: - the first param with all info is at index ```0``` and not ```1``` (i.e. ```sys.argv[0]```) - for me, even if I print index ```0```, ```1```, and so on - I don't get the desired output. It stops after like 20 characters... basically, after the first attribute I am passing in the body. - interestingly enough, when passing 3 params, I cannot get access to the third one. It won't print anything. Microsoft in the meanwhile has gotten back and recommended PowerShell based runbooks. – DevKing Dec 18 '18 at 02:53
  • @DevKing, you can ask anything you want to know. It works at my side. – Ivan Glasenberg Dec 18 '18 at 02:56
  • So, even if I take just your code and only print the argument, the output is this: ```{WebhookName:v2,RequestBody:\t{"Mode"``` not more... I will explore PowerShell runbook. Microsoft confirmed that something seems to be not working w/ Python runbooks. But thanks anyways! – DevKing Dec 18 '18 at 03:07