【问题标题】:Python 3 wsgi - json responsePython 3 wsgi - json 响应
【发布时间】:2017-04-25 19:35:31
【问题描述】:

我使用uwsgi 来提供我的网络内容,它在处理普通的 html 页面时效果很好:

return [b'<html><head></head><body>Hello, world!</body></html>']

但是当我想返回json:

headers = [('content-type', 'application/json')]
test = json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
test1 = bytearray(test, 'utf8')

start_response('200 OK', headers)
return [test1]

网络服务器什么也没返回...没有错误,只是空响应正文...谁能解释我为什么?

谢谢和问候!

【问题讨论】:

  • 能否添加更多信息,例如回溯?
  • 嘿,我更新了我的问题并添加了我的完整代码。没有错误,只是一个空的响应体..
  • 我是第一个遇到这个问题的人吗?

标签: python json response uwsgi wsgi


【解决方案1】:
test = json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])    
test1 = bytes(test, 'utf-8') # or test.encode('utf-8')
start_response('200 OK', headers)
return [test1]

【讨论】:

    【解决方案2】:

    如果这是您的完整代码,那么您忘记导入 json 库

    import json
    def application(environ, start_response):
        headers = [('Content-Type', 'application/json')]
        start_response('200 OK', headers)
        test = json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
        test1 = test.encode('utf-8')      
        return [test1]
    

    【讨论】:

      【解决方案3】:

      测试用这个

      json.dumps({"newdata":"test"})

      【讨论】:

        猜你喜欢
        • 2011-04-13
        • 1970-01-01
        • 2014-06-22
        • 2011-08-05
        • 2022-06-13
        • 1970-01-01
        • 2016-11-19
        • 2015-03-03
        • 1970-01-01
        相关资源
        最近更新 更多