【问题标题】:How to solve this keyerror?如何解决这个关键错误?
【发布时间】:2020-08-22 18:33:34
【问题描述】:
        header = {'Content-Type': 'application/x-www-form-urlencoded'}
        url = 'https://tk9k0fkvyj.execute-api.us-east-2.amazonaws.com/default/top20-predictor'
 
        # make POST request and load response
        r = requests.post(url, params=input_json, headers=header).json()['body']
        result = json.loads(r)
 
        # render the html template sending the variables
        return render_template("score.html", score=result['score'], proba=result['proba'])
 
KeyError: 'body'

请告诉我正确的代码或如何调试..

【问题讨论】:

  • 您确定data 发送为params 而不是data 吗?试试requests.post(url, data=input_json, headers=header).json()['body'],假设input_json是json编码的

标签: python json keyerror


【解决方案1】:

而不是使用

r = requests.post(url, params=input_json, headers=header).json()['body']

只使用

r = requests.post(url, params=input_json, headers=header).json()

您收到错误,因为 r 中没有任何“body”键...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多