当前位置: 首页 > 工具软件 > Viewa > 使用案例 >

Flask报错TypeError: The view function did not return a valid response.

贝杜吟
2023-12-01

访问Flask接口时报错:TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a JSONDecodeError.
反复查看代码,接口代码return的数据格式无误。

最后发现是在post请求Flask接口时,传输的data格式错误。具体如下:

报错代码:

import requests
data = {
    'name':'123'
}
res = requests.post('http://127.0.0.1:5000/media_name',data=data)
print(res.text)

修改后的代码:

import requests
data = {
    'name':'123'
}
res = requests.post('http://127.0.0.1:5000/media_name',data=json.dumps(data))
print(res.text)

解决方法:

在Post请求时,对传输的data数据进行json处理。

 类似资料: