所以我一直在尝试使用以下端点请求 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 请求。
关于为什么会发生这种情况以及如何解决这个问题有什么想法吗?
更新
这是我要尝试的新代码:
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/