【问题标题】:Send dict as response in Python Bottle with custom status code使用自定义状态代码在 Python Bottle 中发送 dict 作为响应
【发布时间】:2012-06-27 08:08:20
【问题描述】:
import bottle
from bottle import route, run

@route('/', method='GET')
def homepage():
    return {'foo' : 'bar'}

if __name__=='__main__':
    bottle.debug(True)
    run(host='0.0.0.0', port= 8080, reloader = True)

此配置将返回一个 json 对象,该对象表示来自主页的带有 HTTP 状态代码 200 的 dict。我应该怎么做才能返回相同的内容,但状态代码为 202?

【问题讨论】:

  • 也许return str({'foo':'bar'})

标签: python json http bottle


【解决方案1】:

可以设置response.status属性:

from bottle import response

@route('/', method='GET')
def homepage():
    response.status = 202
    return {'foo' : 'bar'}

【讨论】:

  • 我已经完成了“从瓶子导入响应/ response.status_code = 202”并得到了 AttributeError。考虑到我使用的是全局应用程序,我应该导入什么?
  • 对不起,我是status,我已经确定了答案。
  • 我知道了:NameError: global name 'response' is not defined
  • 应该先导入response
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 2014-12-30
  • 2016-08-17
  • 2018-05-07
相关资源
最近更新 更多