0

I have an API I'd like to query with multiple values for the same parameter. This is an example of my code:

import requests

headers = { 
  "apikey": "apikey"}

params = (
   ("gl","US"),
   ("hl","en"),
   ("q", "value") 
)

response = requests.get('url', headers=headers, params=params)
print(response.text)

I tried changing "q" in the parameters to a variable, and putting the values for "q" into a list, and then into a tuple.

Example below:

import requests

query = [1, 2, 3, 4, 5)

headers = { 
  "apikey": "apikey"}

params = (
   ("gl","US"),
   ("hl","en"),
   ("q", {value}) 
)

response = requests.get('url', headers=headers, params=params)
print(response.text)

I got an error about not being able to iterate over an unhashable object, and then a genericAlias error with the tuple.

megha
  • 1
  • 1
  • Hi megha! Welcome to Stack Overflow! Could you share the traceback of the error you are receiving? As a note, in `query` you open with `[` and close with `(`, maybe it is just a typo on the code you write here, but check it in the code you run. – Ivanhercaz Jan 18 '23 at 00:42
  • 1
    Short answer: `params` needs to be a dictionary (like `headers`). Either construct it using a dictionary literal, or use `params=dict(params)`. – Selcuk Jan 18 '23 at 00:52

0 Answers0