percent-encoded %2F fail request

huangapple go评论64阅读模式
英文:

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))
    )

huangapple
  • 本文由 发表于 2023年5月25日 05:46:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76327605.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定