python - 不支持获取请求方法 'GET' - Python

标签 python api python-requests

所以我一直在尝试使用以下端点请求 API: http://viatorapi.viator.com/service/search/products?destId=684&apiKey=98765687*****

使用以下 python 代码:

import requests
import json

resp_1 = requests.get("http://viatorapi.viator.com/service/search/products?destId=684&apiKey=98765687*****")

res = resp_1.json()

print(res)

但即使我直接从浏览器尝试请求,我仍然收到 Request method 'GET' not supported 错误。

我已经看了一段时间的文档,它说它应该是一个 POST 请求。

此处:https://docs.viator.com/partner-api/affiliate/technical/#tag/Product-services/paths/~1search~1products/post

关于为什么会发生这种情况以及如何解决这个问题有什么想法吗?

更新

这是我要尝试的新代码:

import requests
import json

j="""{"destId": 684,"seoId": null,"catId": 3,"subCatId": 5318,"startDate": "2018-10-21","endDate": "2019-10-21","dealsOnly": false,"currencyCode": "EUR","topX": "1-3","sortOrder": "TOP_SELLERS"}"""

resp_1 = requests.post("http://viatorapi.viator.com/service/search/products?apiKey=98765687*****", data=json.loads(j))

res = resp_1.json()

print(res)

最佳答案

根据documentation你链接, 很明显,它只接受 /search/products 的 POST 请求。生成 json(如文档中的示例 json)并执行发布请求。

import requests
import json

j="""{
"destId": 684,
"seoId": null,
"catId": 3,
"subCatId": 5318,
"startDate": "2018-10-21",
"endDate": "2019-10-21",
"dealsOnly": false,
"currencyCode": "EUR",
"topX": "1-3",
"sortOrder": "TOP_SELLERS"
}"""
headers={'Content-type':'application/json', 'Accept':'application/json'}
resp_1 = requests.post("http://viatorapi.viator.com/service/search/products?destId=684&apiKey=98765687*****", data=j, headers=headers)
print(resp_1.json())

关于python - 不支持获取请求方法 'GET' - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57332375/

相关文章:

python - 通过 Python 的 "requests"库发出 HTTP 请求时的 404 状态代码。但是页面在浏览器中加载正常

python - OpenCV Python : Reading and setting every pixel too slow

python - 根据条件有效反转列表中的 float

api - 来自数据库表的 REST 资源

c# - 如何在没有分配字母的情况下获得磁盘卷上的可用空间?

python - 使用 python requests 下载文件的中间部分

python - 如何在 PyTorch 中正确实现数值数据的一维 CNN?

python - Azure KeyVault : get_secret() - Python TypeError: string indices must be integers

api - 通过 API POSTMAN 在 Stripe 中创建卡片 token

python - BeautifulSoup 或请求不阅读网页的某些部分