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

时间:2019-08-02 19:23:15

标签: python api python-requests

因此,我一直在尝试使用以下端点请求API: const http = require('http'); createHousehold('Smith', 4); // Creates 'Smith' family with 4 people, and each member has one pet // Not sure how to chain requests function createHousehold(surname, numberOfPeople) { createFamily(surname) .then(familyId => { for (let i = 0; i < numberOfPeople; i++) { createPerson(familyId) .then(personId => createPet(personId)); } }); } function createFamily(surName) { const data = JSON.stringify({ config: { surName } }); const options = { host: 'myProxyHost.com', port: '8080', path: '/v1/family', method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': data.length, }, }; const request = http.request(options, response => { let data = ''; response.on('data', chunk => data += chunk); return (response.on('end', () => JSON.parse(data).id)); }); request.on('error', error => console.log('ERROR - createFamily(): ', error.message)); request.write(data); request.end(); return request; } function createPerson(familyId) { const data = JSON.stringify({ config: { familyId } }); const options = { host: 'myProxyHost.com', port: '8080', path: '/v1/person', method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': data.length, }, }; const request = http.request(options, response => { let data = ''; response.on('data', chunk => data += chunk); return (response.on('end', () => JSON.parse(data).id)); }); request.on('error', error => console.log('ERROR - createPerson(): ', error.message)); request.write(data); request.end(); return request; } function createPet(personId) { const data = JSON.stringify({ config: { personId } }); const options = { host: 'myProxyHost.com', port: '8080', path: '/v1/pet', method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': data.length, }, }; const request = http.request(options, response => { let data = ''; response.on('data', chunk => data += chunk); return (response.on('end', () => JSON.parse(data).id)); }); request.on('error', error => console.log('ERROR - createPet(): ', error.message)); request.write(data); request.end(); return request; }

使用以下python代码:

http://viatorapi.viator.com/service/search/products?destId=684&apiKey=98765687*****

但是即使我直接从浏览器尝试请求,我仍然遇到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) 错误。

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

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

关于这种情况为什么发生以及如何解决的任何想法?

更新

这是我要尝试的新代码:

Request method 'GET' not supported

1 个答案:

答案 0 :(得分:3)

根据您链接的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())