Search code examples
pythoncurlsubprocesstestrail

using testrail api in python script


I have the curl command, but not sure about how to run that in python script.

curl -H "Content-Type: application/json" -u "username:password" -d '{ "name":"something" }' "https://xxxxxxxx"

I'm planning to use subprocess, but the api documents aren't very helpful.

Also does anyone know how to get the sectionId from testrail?


Solution

  • You probably want to use the requests package for this. The curl command translates to something like this:

    import json
    import requests
    
    response = requests.post('https://xxxxxxxx',
                             data=json.dumps({'name': 'something'}),
                             headers={'Content-Type': 'application/json'},
                             auth=('username', 'password'))
    response_data = response.json()
    

    If you really want to use subprocess, you can do something like this:

    import subprocess
    
    curl_args = ['curl', '-H', 'Content-Type: application/json', '-u', 'username:password',
                 '-d', '{ "name":"something" }', 'https://xxxxxxxx']
    curl_output = subprocess.check_output(curl_args)
    

    I consider the latter approach less "Pythonic".