简体   繁体   中英

How can I make this API work with a payload?

I am using this API to list users. One of the parameters I could specify is a team id which is placed in an array. When I try to specify a team id it doesn't work when I put it in the payload, but it works when I change the url to include the team id.

This is the API reference: https://api-reference.pagerduty.com/#!/Users/get_users

Here is what I am basing my code off of: https://github.com/PagerDuty/API_Python_Examples/blob/master/REST_API_v2/Users/list_users.py

This is my code when I try to specify team id in the payload. It doesn't work like this for some reason, but it works when I change the url to url = 'https://api.pagerduty.com/users?team_ids%5B%5D=TEAMID&team_ids%5B%5D=' where in TEAMID I have an actual team id.

with open('config/config.json') as f:
    config = json.load(f)

API_KEY = config['API_KEY']
TEAM_IDS = ['TEAMID']

def list_users():

    url = 'https://api.pagerduty.com/users'
    headers = {
        'Accept': 'application/vnd.pagerduty+json;version=2',
        'Authorization': 'Token token={token}'.format(token=API_KEY)
    }
    payload = {
        'team_ids[]': TEAM_IDS
    }
    r = requests.get(url, headers=headers)

    result = []
    if r.status_code == 200:
        # loops for each user and retrieves their email
        result = [user['email'] for user in r.json()['users']]
        return result
    else:
        return None

I want to get this work by listing team id's in the array and sending it in the payload so that I can list more than one team id and not clutter them all in the url.

Looks like you just need something like this

payload = {
    'team_ids[]': TEAM_IDS 
}
r = requests.get(url, headers=headers, params=payload)

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