英文:
percent-encoded %2F fail request
问题
你好,以下是翻译好的内容:
我有一个字典,我将其作为请求的参数传递:
paylod = {
'latitude': 42.406747,
'longitude': 12.154226,
'timezone': 'Europe%2FBerlin', # 编码问题
'time_format': 'iso8601',
'temperature_unit': 'celsius',
'wind_speed_unit': 'ms',
'precipitation_unit': 'mm',
'start_date': '2023-05-20',
'end_date': '2023-05-23',
'models': ['best_match', 'era5', 'era5_land'],
'hourly': ['temperature_2m', 'relativehumidity_2m', 'dewpoint_2m', 'apparent_temperature', 'pressure_msl', 'surface_pressure', 'precipitation', 'rain', 'snowfall', 'weathercode', 'cloudcover', 'cloudcover_low', 'cloudcover_mid', 'cloudcover_high', 'shortwave_radiation', 'direct_radiation', 'diffuse_radiation', 'direct_normal_irradiance', 'windspeed_10m', 'windspeed_100m', 'winddirection_10m', 'winddirection_100m', 'windgusts_10m', 'et0_fao_evapotranspiration', 'vapor_pressure_deficit', 'soil_temperature_0_to_7cm', 'soil_temperature_7_to_28cm', 'soil_temperature_28_to_100cm', 'soil_temperature_100_to_255cm', 'soil_moisture_0_to_7cm', 'soil_moisture_7_to_28cm', 'soil_moisture_28_to_100cm', 'soil_moisture_100_to_255cm']
}
或者
import request
api_url = "https://archive-api.open-meteo.com/v1/archive"
response = requests.get(
url= api_url,
params=payload
)
问题涉及到Europe%2FBerlin参数(即时区)的编码问题。事实上,在最终的URL中,这个参数Europe%2FBerlin出现为Europe%252FBerlin。
// https://archive-api.open-meteo.com/v1/archive?latitude=42.406747&longitude=12.154226&timezone=Europe%252FBerlin&time_format=iso8601&temperature_unit=celsius&wind_speed_unit=ms&precipitation_unit=mm&start_date=2023-05-20&end_date=2023-05-23&models=best_match&models=era5&models=era5_land&hourly=temperature_2m&hourly=relativehumidity_2m&hourly=dewpoint_2m&hourly=apparent_temperature&hourly=pressure_msl&hourly=surface_pressure&hourly=precipitation&hourly=rain&hourly=snowfall&hourly=weathercode&hourly=cloudcover&hourly=cloudcover_low&hourly=cloudcover_mid&hourly=cloudcover_high&hourly=shortwave_radiation&hourly=direct_radiation&hourly=diffuse_radiation&hourly=direct_normal_irradiance&hourly=windspeed_10m&hourly=windspeed_100m&hourly=winddirection_10m&hourly=winddirection_100m&hourly=windgusts_10m&hourly=et0_fao_evapotranspiration&hourly=vapor_pressure_deficit&hourly=soil_temperature_0_to_7cm&hourly=soil_temperature_7_to_28cm&hourly=soil_temperature_28_to_100cm&hourly=soil_temperature_100_to_255cm&hourly=soil_moisture_0_to_7cm&hourly=soil_moisture_7_to_28cm&hourly=soil_moisture_28_to_100cm&hourly=soil_moisture_100_to_255cm
{
"reason": "Invalid timezone",
"error": true
}
英文:
I have a dictionary that I pass as a parameter in request:
paylod = {
'latitude': 42.406747,
'longitude': 12.154226,
'timezone': 'Europe%2FBerlin', # encoding problem
'time_format': 'iso8601',
'temperature_unit': 'celsius',
'wind_speed_unit': 'ms',
'precipitation_unit': 'mm',
'start_date': '2023-05-20',
'end_date': '2023-05-23',
'models': ['best_match', 'era5', 'era5_land'],
'hourly': ['temperature_2m', 'relativehumidity_2m', 'dewpoint_2m', 'apparent_temperature', 'pressure_msl', 'surface_pressure', 'precipitation', 'rain', 'snowfall', 'weathercode', 'cloudcover', 'cloudcover_low', 'cloudcover_mid', 'cloudcover_high', 'shortwave_radiation', 'direct_radiation', 'diffuse_radiation', 'direct_normal_irradiance', 'windspeed_10m', 'windspeed_100m', 'winddirection_10m', 'winddirection_100m', 'windgusts_10m', 'et0_fao_evapotranspiration', 'vapor_pressure_deficit', 'soil_temperature_0_to_7cm', 'soil_temperature_7_to_28cm', 'soil_temperature_28_to_100cm', 'soil_temperature_100_to_255cm', 'soil_moisture_0_to_7cm', 'soil_moisture_7_to_28cm', 'soil_moisture_28_to_100cm', 'soil_moisture_100_to_255cm']
}
or
import request
api_url = "https://archive-api.open-meteo.com/v1/archive"
response = requests.get(
url= api_url,
params=payload
)
The problem concerns the encoding of the Europe%2FBerlin parameter (i.e., timezone). In fact, in the final url, which causes the request to fail, the parameter Europe%2FBerlin appears as Europe%252FBerlin
// https://archive-api.open-meteo.com/v1/archive?latitude=42.406747&longitude=12.154226&timezone=Europe%252FBerlin&time_format=iso8601&temperature_unit=celsius&wind_speed_unit=ms&precipitation_unit=mm&start_date=2023-05-20&end_date=2023-05-23&models=best_match&models=era5&models=era5_land&hourly=temperature_2m&hourly=relativehumidity_2m&hourly=dewpoint_2m&hourly=apparent_temperature&hourly=pressure_msl&hourly=surface_pressure&hourly=precipitation&hourly=rain&hourly=snowfall&hourly=weathercode&hourly=cloudcover&hourly=cloudcover_low&hourly=cloudcover_mid&hourly=cloudcover_high&hourly=shortwave_radiation&hourly=direct_radiation&hourly=diffuse_radiation&hourly=direct_normal_irradiance&hourly=windspeed_10m&hourly=windspeed_100m&hourly=winddirection_10m&hourly=winddirection_100m&hourly=windgusts_10m&hourly=et0_fao_evapotranspiration&hourly=vapor_pressure_deficit&hourly=soil_temperature_0_to_7cm&hourly=soil_temperature_7_to_28cm&hourly=soil_temperature_28_to_100cm&hourly=soil_temperature_100_to_255cm&hourly=soil_moisture_0_to_7cm&hourly=soil_moisture_7_to_28cm&hourly=soil_moisture_28_to_100cm&hourly=soil_moisture_100_to_255cm
{
"reason": "Invalid timezone",
"error": true
}
答案1
得分: 1
问题在于request.get将"&"符号转换成了URL编码中的"%25"。将'时区':'Europe%2FBerlin'更改为'时区':'Europe/Berlin'应该可以解决。
英文:
The problem is that request.get convert "%" into "%25" in URL encoding.
Change 'timezone': 'Europe%2FBerlin' into 'timezone': 'Europe/Berlin' should work.
答案2
得分: 0
use from urllib.parse import unquote
to convert the character %2f
to /
format
from urllib.parse import unquote
import requests
import json
api_url = "https://archive-api.open-meteo.com/v1/archive"
response = requests.get(
url=api_url,
params=json.loads(unquote(payload))
)
英文:
use from urllib.parse import unquote
to convert the character %2f
to /
format
from urllib.parse import unquote
import request
import json
api_url = "https://archive-api.open-meteo.com/v1/archive"
response = requests.get(
url= api_url,
params=json.loads(unquote(payload))
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论