簡體   English   中英

類型錯誤:“數據”是此函數的無效關鍵字參數

[英]TypeError: 'data' is an invalid keyword argument for this function

我正在嘗試修改函數request以根據 api 調用采用不同的參數。 例如:在post_categories ,我需要它發送包含我要發布的正文的第三個參數data ,但get_categories函數不需要第三個參數。 我在請求函數中添加了**kwargs ,但這是我得到的錯誤: TypeError: 'data' is an invalid keyword argument for this function

class ApiGateway():
    base_url = 'https://api.com/v3/'

    def request(self, method, endpoint, **kwargs):
        url = f'{self.base_url}{endpoint}'
        kwargs.setdefault('headers', {})
        kwargs['headers'].update({
            'Authorization': f'Bearer ${self.token}',
            'Accept': 'application/json',
        })
        response = requests.request(method, url, **kwargs)
        return response.json()

    def get_categories(self, merchant_id):
        endpoint = f'merchants/{merchant_id}/categories'
        return self.request('GET', endpoint)

    def post_categories(self, merchant_id):
        update = {
            'payment_method': {
                'token': 1234,
                'data': '123556'
            }
        }    
        endpoint = f'merchants/{merchant_id}/categories'
        return self.request('POST', endpoint, data=json.dumps(update)) 

我找到了解決方案。 我必須指定我想傳遞給request函數的數據類型,而不是將數據作為 kwargs 參數傳遞。 我剛剛更新了post_categories函數的這一部分return self.request('POST', endpoint, data=json.dumps(update))所以現在函數看起來像這樣

 def post_categories(self, merchant_id): update = { 'payment_method': { 'token': 1234, 'data': '123556' } } endpoint = f'merchants/{merchant_id}/categories' return self.request('POST', endpoint, json=update) # <-- updated third parameter

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM