【问题标题】:API request works in browser, but, doesn't work in postman or python requests moduleAPI 请求在浏览器中有效,但在邮递员或 python 请求模块中无效
【发布时间】:2021-06-09 17:54:55
【问题描述】:

我正在尝试请求此 API:https://iso19139echnap.geocat.live/geonetwork/srv/api/0.1/groups/11864 如果我将其作为直接 URL 输入,它可以在浏览器中使用。但它在 Python Request 模块或 Post 人中不起作用。

Python 代码如下所示:

import requests

payload = {
    'username': 'corey',
    'password': 'testing'
}
r = requests.get('https://iso19139echnap.geocat.live/geonetwork/srv/api/0.1/groups/11864')

print(r)
--------------------------------------------------------
returns
<Response [400]>

【问题讨论】:

    标签: json api python-requests postman


    【解决方案1】:
    import requests
    
    url = "https://iso19139echnap.geocat.live/geonetwork/srv/api/0.1/groups/11864"
    
    payload = {}
    headers = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'en-IE,en-GB;q=0.9,en-US;q=0.8,en;q=0.7',
        }
    
    response = requests.request("GET", url, headers=headers, data=payload)
    
    print(response.text)
    

    你必须添加接受编码头

    在邮递员中也添加相同的,它工作正常

    【讨论】:

    • 你怎么知道要为标题“Accept”提供什么参数?
    • 转到开发工具 > 网络选项卡并复制请求标头并通过单击 bul 编辑添加到邮递员标头
    • 禁用每个标题并在禁用哪个标题后查看问题出现
    • 至于什么参数,你要返回什么格式的数据?它可能能够根据该字段的值重新格式化
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-26
    • 2021-08-23
    相关资源
    最近更新 更多