pythonrestcurlpython-requestsrt

I need help converting this REST API Curl command to Python requests


I'm new around here, and rather new to all that is coding to be honest.

I'm trying to create a Pyton script to search for items from a Request Tracker asset database using REST API.

So far I got this Curl command:

curl    -X POST \
-H "Content-Type: application/json" \
-d '[{ "field" : "Owner", "operator" : "LIKE", "value" : "NAME" },{"field":"Catalog", "value":"1"}]' \
-H 'Authorization: token MY_TOKEN' \
'https://RT_URL/rt/REST/2.0/assets'

It returns a nice JSON with results from RT_URL where Owner is matching NAME using token MY_TOKEN.

But I don't know how to code this in Python. I have a script that use the requests library to fetch is using a simple URL request, but I can't figure out how to implement the search fields.

I've looked all over to find a sample, but I can't get it to work. I haven't found any info on how to authenticate in requests using a token.

Anyway, thanks in advance for any respons :)


Solution

  • Try this code

    import requests
    
    headers = {
        'Content-Type': 'application/json',
        'Authorization': 'token TOKEN',
    }
    
    data = '[{ "field" : "value"}] ......'
    
    response = requests.post('YOUR_URL', headers=headers, data=data)