最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Updating requests from version 2.31.0 to 2.32.3 breaks my code - Stack Overflow

programmeradmin1浏览0评论

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
Add a comment  | 

2 Answers 2

Reset to default 1

The 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.

发布评论

评论列表(0)

  1. 暂无评论