Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python script – API call fails due to JSON arguments

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading