Frage

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

War es hilfreich?

Lösung

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")
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top