Mithril
Mithril

Reputation: 13808

How to return a json response in twisted?

When I am using django: I always do

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.

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

It return raw string

Upvotes: 3

Views: 3970

Answers (1)

Jean-Paul Calderone
Jean-Paul Calderone

Reputation: 48345

Encoded JSON is a (byte) string.

If your question is "How do I set the Content-Type of the response to application/json?" then the answer is:

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

Upvotes: 7

Related Questions