简体   繁体   English

如何返回扭曲的json响应?

[英]How to return a json response in twisted?

When I am using django: I always do 当我使用django时:我总是这样做

return HttpResponse(json.dumps(result), mimetype='application/json')

How can I do this in twisted? 我该如何扭曲呢? Official document do not say this. 官方文件不说这个。

document here 在这里记录

Only Serving WSGI Applications can set mimetype .But I want to process GET and POST There is no more example, and I searched with nothing found. 只有Serving WSGI Applications可以设置mimetype 。但是我想处理GET和POST没有更多示例了,我没有找到任何搜索结果。

from twisted.web import resource
class MyGreatResource(resource.Resource):
    def render_GET(self, request):
        return "xxxx"

It return raw string 返回原始字符串

Encoded JSON is a (byte) string. 编码的JSON 一个(字节)字符串。

If your question is "How do I set the Content-Type of the response to application/json ?" 如果您的问题是“如何将响应的Content-Type设置为application/json ?” then the answer is: 那么答案是:

request.responseHeaders.addRawHeader(b"content-type", b"application/json")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM