当我尝试在 URL 有破折号(-)时进行 get 调用时,Ssleepber Python 库失败



我正在使用python的ssleepber库对服务进行HTTP调用。这就是它的样子。我需要 https://sample-billing-api.test/2/billing-accounts?id=2169 对此 URL 发出获取请求。

当我运行它时,我收到错误名称错误:未定义名称"帐户"。

import slumber
class ParseAuth(AuthBase):
def __call__(self, r):
r.headers['x-api-key'] = '<API KEY>'
r.headers['Content-Type'] = 'application/json'
return r
api = slumber.API('https://sample-billing-api.test/2/', append_slash=False, auth=ParseAuth())
response = api.billing-accounts.get(id=2169)

api.billing-accounts.get(id=2169( 行不起作用。

一种解决方案是切换到 python 请求包,就像这样的事情一样。这种方法有效。但是我需要使用 ssleepber 包,因为我一直在使用 Ssleepber 包进行所有 API 调用。我也写了装饰器来处理睡眠反应。

import requests
headers = {
'x-api-key': '<api_key>',
'Content-Type': 'application/json'
}
with requests.session() as s:
res = s.get(
'https://sample-billing-api.test/2/billing-accounts?id=2169',
headers=headers
)
s.close()

提前谢谢。

这个解决方案终于奏效了...

import slumber
class ParseAuth(AuthBase):
def __call__(self, r):
r.headers['x-api-key'] = '<API KEY>'
r.headers['Content-Type'] = 'application/json'
return r
api = slumber.API('https://sample-billing-api.test/2/', append_slash=False, 
auth=ParseAuth())
response = getattr(self.api, "billing-accounts").get(id=2169)

最新更新