Python script – API call fails due to JSON arguments
Question:
I am trying to pull reports through a (shadowserver) API – doc here:
The python script is written by ShadowServer and I have tested it with the simple usage example they are providing:
$ ./call-api.py test/ping '{}'
{"pong":"2020-10-26 23:06:37"}
Thus far, everything works fine. I can submit additional calls and I get a response back.
However, when i try to pass a JSON parameter to the script (that is executed in command prompt), such as the example below:
os.system('''[some_directory]\call-api.py reports/query '{ "help":true }' pretty''')
I get the following result:
JSON Exception: Expecting value: line 1 column 1 (char 0)
I believe that it is an issue with the quotes – single/double/triple.. but I’ve tried everything and it keeps returning the same result.
Any ideas?
Many thanks!
Answers:
You might want to try subprocess
to see if you get the same result, e.g. like this
import subprocess
subprocess.Popen([
"[some_directory]\call-api.py",
"reports/query",
'{"help": true}',
"pretty",
])
or use json.dumps
to not worry about any quoting
import subprocess
import json
subprocess.Popen([
"[some_directory]\call-api.py",
"reports/query",
json.dumps({"help": True}),
"pretty",
])
SitiSchu came up with the json.dumps
idea.
I am trying to pull reports through a (shadowserver) API – doc here:
The python script is written by ShadowServer and I have tested it with the simple usage example they are providing:
$ ./call-api.py test/ping '{}'
{"pong":"2020-10-26 23:06:37"}
Thus far, everything works fine. I can submit additional calls and I get a response back.
However, when i try to pass a JSON parameter to the script (that is executed in command prompt), such as the example below:
os.system('''[some_directory]\call-api.py reports/query '{ "help":true }' pretty''')
I get the following result:
JSON Exception: Expecting value: line 1 column 1 (char 0)
I believe that it is an issue with the quotes – single/double/triple.. but I’ve tried everything and it keeps returning the same result.
Any ideas?
Many thanks!
You might want to try subprocess
to see if you get the same result, e.g. like this
import subprocess
subprocess.Popen([
"[some_directory]\call-api.py",
"reports/query",
'{"help": true}',
"pretty",
])
or use json.dumps
to not worry about any quoting
import subprocess
import json
subprocess.Popen([
"[some_directory]\call-api.py",
"reports/query",
json.dumps({"help": True}),
"pretty",
])
SitiSchu came up with the json.dumps
idea.