pythoncurlcommand-linepython-requestsicinga2

Converting curl command to python?


I have this curl command, and I would like to transform it into a python request (including the query):

curl -u user:pwd123 -s -G --data-urlencode "q=GET services\nColumns: column1 column2 column3\nFilter: column1 ~~ stringtofilter\nFilter: column2 ~~ string2\nFilter: string3 ~~ string3tofilter" http://myendpoint.inet/api/query?

To convert it to python I can do this:

import requests

response = requests.get('http://myendpoint.inet/api/query', auth=('user', 'pwd123'))

But how can I include my query inside?


Solution

  • I found the way to do that:

    response = requests.get('http://myendpoint.inet/api/query', params = "q=GET services\nColumns: column1 column2 column3\nFilter: column1 ~~ stringtofilter\nFilter: column2 ~~ string2\nFilter: string3 ~~ string3tofilter", auth=("user", "pwd123"))
    

    To see if the command is successful: print (response) It must return <Response [200]>

    To see the response:

    print (response.text)