如何解决这个关键错误?

问题描述

        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'

请告知我正确的代码或如何调试。

解决方法

代替使用

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

仅使用

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

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...