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

2024-04-29 20:04:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用python的sleber库对服务进行HTTP调用。这就是它的样子。我需要对这个URLhttps://sample-billing-api.test/2/billing-accounts?id=2169发出get请求。你知道吗

当我运行这个时,我得到一个错误NameError:name'accounts'没有定义。你知道吗

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.计费-帐户。获取(id=2169)线路不工作。你知道吗

一种解决方案是切换到python请求包,这样做。这种方法有效。但我需要使用slumber包,因为我一直在为所有API调用使用slumber包。我也写了装饰处理睡眠反应。你知道吗

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()

提前谢谢。你知道吗


Tags: samplekeytestimportapiidgettype
1条回答
网友
1楼 · 发布于 2024-04-29 20:04:50

这个解决方案终于奏效了。。。你知道吗

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)

相关问题 更多 >