i have a simple api call that should return me the news based on stock tickers i pass in the get request. But I would like to pass the data array to the API call. How do I pass all the values in the data list to the API call request?
JavaScript
x
7
1
def main():
2
3
data = ["AAPL", "SQ", "PLTR"]
4
// API call which I would like to pass values in data
5
response = requests.get("https://stocknewsapi.com/api/v1tickers=data&items=50&date=last7days&token=myapikey")
6
7
Advertisement
Answer
You can use params
in requests
to use arguments in url
JavaScript
1
16
16
1
import requests
2
3
data = ["AAPL", "SQ", "PLTR"]
4
data_str = ",".join(data)
5
6
url = "https://stocknewsapi.com/api/v1"
7
8
payload = {
9
"tickers": data_str,
10
"items": 50,
11
"date": "last7days",
12
"token": "myapikey",
13
}
14
15
response = requests.get(url, params=payload)
16
Eventually you can use string formatting with {}
and .format(data_str)
JavaScript
1
4
1
data_str = ",".join(data)
2
3
url = "https://stocknewsapi.com/api/v1?tickers={}&items=50&date=last7days&token=myapikey".format(data_str)
4
or f-string
with {data_str}
JavaScript
1
4
1
data_str = ",".join(data)
2
3
url = f"https://stocknewsapi.com/api/v1?tickers={data_str}&items=50&date=last7days&token=myapikey"
4