Skip to content Skip to sidebar Skip to footer

How To Handle Post Requests In Twisted

I have a very simple twisted script where you can handle POST requests: class FormPage(Resource): isLeaf = True def render_GET(self, request): return b'''

Solution 1:

I found a solution using request.content.read()

defrender_POST(self, request):
    return'<html><body>You submitted: %s</body></html>' % (request.content.read())

It might not be the best solution, but It worked for me. Please comment If you have a better solution, thanks!

Solution 2:

Found the solution. I got stuck here for a long time doing the "O'Reily Twisted" in Python3. Here's what worked for me:

defrender_POST(self, request):
    return_value = "<html><body>You submitted: %s </body></html>" % (cgi.escape(str(request.args[b"form-field"][0], 'utf-8')))
    returnstr.encode(return_value)

I guess the first reason I had trouble was that the form fields were that can be extracted in python code were in byte string. Only after I checked the request args that I realized that. I suppose in python2, it was regular string.

Post a Comment for "How To Handle Post Requests In Twisted"