【发布时间】:2017-04-03 09:40:27
【问题描述】:
(注意:这是duplicate,我投票决定关闭它,但它可能永远不会发生,所以我将信息放在显眼位置,因为我不知道是否看到关于可能重复的黄色信息横幅所有人或只是我)
我想使用bottle 返回列表的 JSON 表示:
import bottle
def ret_dict():
return {
"who": {"name": "John"}
}
def ret_list():
return [
{"name": "John"}
]
app = bottle.Bottle()
app.route('/dict', 'GET', ret_dict)
app.route('/list', 'GET', ret_list)
app.run()
调用http://127.0.0.1:8080/dict 返回{"who": {"name": "John"}} 并且Content-Type 设置为application/json。没关系。
调用http://127.0.0.1:8080/list 返回500:
错误:500 内部服务器错误对不起,请求的 URL
'http://127.0.0.1:8080/list'导致错误:
不支持的响应类型:
Python 控制台上没有错误也没有回溯。
同时可以将列表序列化为JSON:
>>> import json
>>> json.dumps([{"name": "John"}])
'[{"name": "John"}]'
为什么bottle 在尝试返回 list 时会引发错误?(并且很高兴返回 dict)
【问题讨论】:
-
问瓶作者他为什么这样做。