简体   繁体   中英

Translate curl command to python requests.get

I have the following curl command which I can use to retrieve a list of users from a specific group in PagerDuty:

curl -H "Accept: application/vnd.pagerduty+json;version=2" -H "Authorization: Token token=xxx" -X GET --data-urlencode "team_ids[]=abc" 'https://api.pagerduty.com/users'

How can I translate this exact command to run in a python get.requests() command? I can't seem to get it to work. This is what I'm currently trying but it doesn't filter on the group:

response = requests.get(
        'https://api.pagerduty.com/users', params = {"data-urlencode": "team_ids[]=abc"}, headers={'Accept': 'application/vnd.pagerduty+json;version=2','Authorization': 'Token token=xxx'}
    )

    )

Thanks!

Simply omit --data-urlencode from your params:

import requests

params = {
    'team_ids[]':"abc",
    "offset": '0',
}
headers = {
    'Accept': 'application/vnd.pagerduty+json;version=2',
    'Authorization': 'Token token=xxx',
}

response = requests.get('https://api.pagerduty.com/users', params=params, headers=headers)

if first solution is hard for you u can use one of websites that convert curl command to pytho/php etc... code

curlconverter.com

https://sqqihao.github.io/trillworks.html

https://www.scrapingbee.com/curl-converter/python/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM