使用 Python 的请求库,无法调用 API 以正确格式化
Using Python's Requests library, cannot get API call to properly format
作为参考,我是一名尝试学习 Python 并使用英国航空公司的 Flight Offer Basic API 的新手,可在此处找到其文档:
https://developer.ba.com/docs/api/Flight_Offer_Basic
使用 docs.python-requests 中的 Requests 库文档,我正在尝试构建实际有效的 a matching API call,但在不包括我的 API 时我得到了 403键和查询的必要参数(这很有意义),但是当我同时包含参数和 API 键时是 400...这没有意义,除了当我查看我的 API 调用的样子以及他们提供的成功示例的样子:
(注意:HTTP:已删除,因为我目前不能 post 超过两个链接)
我的:
//api.ba.com/rest-v1/v1/flightOfferBasic?range=monthLow&departureCity=LON&arrivalCity=NYC&cabin=economy&journeyType=roundTrip
他们的:
//api.ba.com/rest-v1/v1/flightOfferBasic;departureCity=LON;arrivalCity=NYC;cabin=economy;journeyType=roundTrip;range=monthLow.json
已编辑:2015 年 6 月 6 日 12:20PM PST;我在一些测试中弄乱了代码,但没有意识到我没有将其还原,特别是 'journeyType'.
这是我的代码:
import requests
import pprint
parameters = { 'departureCity': 'LON',
'arrivalCity': 'NYC',
'cabin': 'economy',
'journeyType': 'roundTrip',
'range': 'monthLow'
}
endpoint = 'https://api.ba.com/rest-v1/v1/flightOfferBasic'
headers = {'client-key': 'h6z...'}
response = requests.get(endpoint, params = parameters, headers = headers)
data = response.json
pprint.pprint(data)
我注意到调用中的每个参数都有一个“&”连接,成功的调用有一个“;”在调用中加入每个参数,这似乎是我得到 400 "Sent when the consumer sends a malformed request to the service. The actual error message is returned in the response body".
的原因
当我检查响应正文时,我被告知:
{"Errors":{"Error":"invalid range"}}
但我已经双重和三次检查这是否是正确的键值和字符串。当我删除要测试的范围时,我被告知 "invalid cabin"。当我移除机舱时,我仍然被告知 "invalid cabin"。但我已经双重和三次检查这是否是适用于机舱的正确键值和字符串。
成功的调用最后也有“.json”,但我认为"response.json"调用应该将返回的数据转换为JSON对象...对?另一件奇怪的事情是,然后按照文档的建议将 "format" 参数作为“.json”传递:
parameters = { 'departureCity': 'LON',
'arrivalCity': 'NYC',
'cabin': 'economy',
'journeyType': 'roundTrip',
'range': 'monthLow',
'format': '.json'
}
我在 API 电话中得到了这个 URL:
//api.ba.com/rest-v1/v1/flightOfferBasic?format=.json&arrivalCity=NYC&range=monthLow&journeyType=roundTrip&departureCity=LON&cabin=economy
它作为第一个参数注入,当它应该是最后一个时,当它是我的 "parameters" 字典中的最后一个参数时也是如此。
知道我哪里出错了吗?
唉。那是一个可怕的设计 API.
如您所见,问题在于他们希望参数以分号分隔。这意味着它们根本不是查询字符串的一部分,这是当您给它一个 params
字典时请求所做的,而是实际 URL 路径的一部分。
您需要手动构建它,例如通过字符串插值:
parameters = { 'departureCity': 'LON',
'arrivalCity': 'NYC',
'cabin': 'economy',
'journeyType': 'oneWay',
'range': 'monthLow'
}
path = ';'.join('{}={}'.format(k, v) for k, v in parameters.items())
endpoint = 'https://api.ba.com/rest-v1/v1/flightOfferBasic'
url = '{};{}'.format(endpoint, path)
response = requests.get(url, headers=headers)
作为参考,我是一名尝试学习 Python 并使用英国航空公司的 Flight Offer Basic API 的新手,可在此处找到其文档:
https://developer.ba.com/docs/api/Flight_Offer_Basic
使用 docs.python-requests 中的 Requests 库文档,我正在尝试构建实际有效的 a matching API call,但在不包括我的 API 时我得到了 403键和查询的必要参数(这很有意义),但是当我同时包含参数和 API 键时是 400...这没有意义,除了当我查看我的 API 调用的样子以及他们提供的成功示例的样子:
(注意:HTTP:已删除,因为我目前不能 post 超过两个链接)
我的:
//api.ba.com/rest-v1/v1/flightOfferBasic?range=monthLow&departureCity=LON&arrivalCity=NYC&cabin=economy&journeyType=roundTrip
他们的:
//api.ba.com/rest-v1/v1/flightOfferBasic;departureCity=LON;arrivalCity=NYC;cabin=economy;journeyType=roundTrip;range=monthLow.json
已编辑:2015 年 6 月 6 日 12:20PM PST;我在一些测试中弄乱了代码,但没有意识到我没有将其还原,特别是 'journeyType'.
这是我的代码:
import requests
import pprint
parameters = { 'departureCity': 'LON',
'arrivalCity': 'NYC',
'cabin': 'economy',
'journeyType': 'roundTrip',
'range': 'monthLow'
}
endpoint = 'https://api.ba.com/rest-v1/v1/flightOfferBasic'
headers = {'client-key': 'h6z...'}
response = requests.get(endpoint, params = parameters, headers = headers)
data = response.json
pprint.pprint(data)
我注意到调用中的每个参数都有一个“&”连接,成功的调用有一个“;”在调用中加入每个参数,这似乎是我得到 400 "Sent when the consumer sends a malformed request to the service. The actual error message is returned in the response body".
的原因当我检查响应正文时,我被告知:
{"Errors":{"Error":"invalid range"}}
但我已经双重和三次检查这是否是正确的键值和字符串。当我删除要测试的范围时,我被告知 "invalid cabin"。当我移除机舱时,我仍然被告知 "invalid cabin"。但我已经双重和三次检查这是否是适用于机舱的正确键值和字符串。
成功的调用最后也有“.json”,但我认为"response.json"调用应该将返回的数据转换为JSON对象...对?另一件奇怪的事情是,然后按照文档的建议将 "format" 参数作为“.json”传递:
parameters = { 'departureCity': 'LON',
'arrivalCity': 'NYC',
'cabin': 'economy',
'journeyType': 'roundTrip',
'range': 'monthLow',
'format': '.json'
}
我在 API 电话中得到了这个 URL:
//api.ba.com/rest-v1/v1/flightOfferBasic?format=.json&arrivalCity=NYC&range=monthLow&journeyType=roundTrip&departureCity=LON&cabin=economy
它作为第一个参数注入,当它应该是最后一个时,当它是我的 "parameters" 字典中的最后一个参数时也是如此。
知道我哪里出错了吗?
唉。那是一个可怕的设计 API.
如您所见,问题在于他们希望参数以分号分隔。这意味着它们根本不是查询字符串的一部分,这是当您给它一个 params
字典时请求所做的,而是实际 URL 路径的一部分。
您需要手动构建它,例如通过字符串插值:
parameters = { 'departureCity': 'LON',
'arrivalCity': 'NYC',
'cabin': 'economy',
'journeyType': 'oneWay',
'range': 'monthLow'
}
path = ';'.join('{}={}'.format(k, v) for k, v in parameters.items())
endpoint = 'https://api.ba.com/rest-v1/v1/flightOfferBasic'
url = '{};{}'.format(endpoint, path)
response = requests.get(url, headers=headers)