skip to Main Content

i want to request data from an API but I’m struggling to get multiple params within one request.

I have the following line of code:

r = requests.get('https://api.delta.exchange/v2/tickers?contract_types= call_options, put_options')

According to the API documentation the params for each contract_type can be called by separating with a comma. While using the line above it will only return the first called param in that case call_options. I tried separating with an & with the same effect. For some reason using request.get(url, params={call_options,put_options}) results in the error that call_options is not defined.

Probably a very trivial problem but I’m thankful for any help or suggestion.

enter image description here

2

Answers


  1. The correct argument for the params parameter would be a dict whose key is the parameter name and the value would be the comma-separated string.

    r = requests.get('https://api.delta.exchange/v2/tickers',
                     param={'contract_types': 'call_options,put_options'})
    
    Login or Signup to reply.
  2. I cannot reproduce why your line of code should only give you "call options".

    The following example and the according output shows that the API is returning the expected types.

    urls = ['https://api.delta.exchange/v2/tickers?contract_types=call_options', 
    'https://api.delta.exchange/v2/tickers?contract_types=put_options',
    'https://api.delta.exchange/v2/tickers?contract_types=call_options,put_options']
    
    for url in urls:
        r = requests.get(url)
        print(f"URL: {url}")
        print(f"Occurences of call_options: {r.text.count('call_option')}")
        print(f"Occurences of put_options: {r.text.count('put_options')}")
        print("")
    

    Output:

    URL: https://api.delta.exchange/v2/tickers?contract_types=call_options
    Occurences of call_options: 366
    Occurences of put_options: 0
    
    URL: https://api.delta.exchange/v2/tickers?contract_types=put_options
    Occurences of call_options: 0
    Occurences of put_options: 372
    
    URL: https://api.delta.exchange/v2/tickers?contract_types=call_options,put_options
    Occurences of call_options: 366
    Occurences of put_options: 372
    

    In general, you should avoid using spaces in URLs.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search