来自 PythonAnyWhere 白名单的 api 请求站点的错误 400

问题描述 投票:0回答:0

我正在尝试使用 Flask 为 REST API 创建简单的演示示例。当我从 localhost 测试它时,我的代码是好的。但是使用 Pythonanywhere 免费帐户部署,我有 400 错误。

   response = requests.get(
        'https://api.openweathermap.org/data/2.5/weather',
        params=params
    )

这里是证明 - api.openweathermap.org 在白名单中

有什么帮助吗?

这是最终代码的一部分

import requests

URL = "http://demoapi.pythonanywhere.com"

def printer(method, requests_answer):
    print(F"{requests_answer=} for {method=}")
    if requests_answer:
        print(F"{requests_answer.text}")
        try:
            print(F"{requests_answer.json()=}")
        except:
            print('json is not present')
    print("\n", 30*"*^")
    

response = requests.get(URL)
printer('GET' ,response)

response = requests.get(URL + '/data/weather/Львів')
printer('GET', response)

params = dict(city_name='Львів')
response = requests.get(URL + '/data/weather', params=params)
printer('GET', response)

所以

requests.get(URL)
requests.get(URL + '/data/weather/Львів')
仍然可以,但是
 requests.get(URL + '/data/weather', params=params)
给出 400.

当给我这个问题的减号时,请说任何...

python api flask python-requests pythonanywhere
© www.soinside.com 2019 - 2024. All rights reserved.