admin管理员组文章数量:1394217
I have some code that uses the requests library to access some grid information from the Spanish electric system. There is open access if you ask for an API key. I created the code under the 2.31.0 version of requests, using the following call:
import requests
url = ''
api_key = 'my_apy_key'
headers = headers = {
'Accept':'application/json; application/vnd.esios-api-v2+json',
'Content-Type':'application/json','Host':'api.esios.ree.es',
'x-api-key': api_key}
params = {'start_date': '2025/03/01T00', 'end_date': '2025/03/12T23'}
resp = requests.get( url, headers=headers, params = params)
This, which still works under 2.31.0, returns a 403 when updating requests. I have no idea what is the change. I have very important code which requires this to work, and I would at least like to know what could I do if updating requests at some point is absolutely necessary. Any ideas?
I have some code that uses the requests library to access some grid information from the Spanish electric system. There is open access if you ask for an API key. I created the code under the 2.31.0 version of requests, using the following call:
import requests
url = 'https://api.esios.ree.es/indicators/1002'
api_key = 'my_apy_key'
headers = headers = {
'Accept':'application/json; application/vnd.esios-api-v2+json',
'Content-Type':'application/json','Host':'api.esios.ree.es',
'x-api-key': api_key}
params = {'start_date': '2025/03/01T00', 'end_date': '2025/03/12T23'}
resp = requests.get( url, headers=headers, params = params)
This, which still works under 2.31.0, returns a 403 when updating requests. I have no idea what is the change. I have very important code which requires this to work, and I would at least like to know what could I do if updating requests at some point is absolutely necessary. Any ideas?
Share Improve this question edited Mar 12 at 15:45 jonrsharpe 122k30 gold badges268 silver badges476 bronze badges asked Mar 12 at 15:42 lucassculplucassculp 816 bronze badges 1- Are you able to access the API with curl – Adon Bilivit Commented Mar 12 at 16:46
2 Answers
Reset to default 1The API requires that you pass a User-Agent.
Given a valid API Key, the following will run without error:
import requests
from apikeys import ESIOS_API_KEY
url = "https://api.esios.ree.es/indicators/1002"
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36",
"Accept": "application/json; application/vnd.esios-api-v1+json",
"Content-Type": "application/json",
"x-api-key": ESIOS_API_KEY,
}
params = {"start_date": "2025/03/01T00", "end_date": "2025/03/12T23"}
with requests.get(url, headers=headers, params=params) as response:
response.raise_for_status()
data = response.json()
geo_names = set()
for v in data.get("indicator", {}).get("values", []):
geo_names.add(v.get("geo_name"))
print(geo_names)
Output:
{'Melilla', 'Península', 'Ceuta', 'Baleares', 'Canarias'}
The response object contains a .request property, which you can use to examine the request that was actually sent.
https://requests.readthedocs.io/en/latest/api/#requests.Response.request
Comparing these between versions should help you discover where the issue is coming from.
本文标签: pythonUpdating requests from version 2310 to 2323 breaks my codeStack Overflow
版权声明:本文标题:python - Updating requests from version 2.31.0 to 2.32.3 breaks my code - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744743535a2622752.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论