(注意:这是一个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
:
Error: 500 Internal Server Error Sorry, the requested URL
' http://127.0.0.1:8080/list ' caused an error:
Unsupported response type: < class 'dict' >
有Python的控制台上没有错误,也没有回溯。
与此同时列表可序列化到JSON:
>>> import json
>>> json.dumps([{"name": "John"}])
'[{"name": "John"}]'
为什么bottle
提高试图返回list
时错误?(并愉快地返回dict
)
问瓶子作者为什么这么做。 – furas