首页 > 解决方案 > 仅在使用 python-requests 时从 Typeform API 获取 406 Not Acceptable

问题描述

Typeform“以 JSON 形式查看表单”(例如:https ://api.typeform.com/forms/rGVAYs No Authentiction Needed)API 端点406 Not Acceptable在通过 pythonrequests 库连接时返回错误,但没有来自其他客户端。

python请求发送的请求头如下:

Accept: */*
Accept-Encoding: gzip, deflate
User-Agent: python-requests/2.21.0

标签: python-requestshttp-status-code-406typeform

解决方案


出于某种我不知道的原因,Typeform“以 JSON 形式查看表单”API 端点似乎拒绝来自 pythonrequests库的请求(仅?),发送 HTTP 错误406 Not Acceptable

对此的解决方案是添加一个指定不同的标头User-Agent,本质上是欺骗用户代理

import requests

header = {
    "User-Agent": "Mozilla/5.0",
}
resp = requests.get("https://api.typeform.com/forms/rGVAYs", headers=header)
print (resp.content)

推荐阅读