Accepting a POST request and return successful response

115 views
Skip to first unread message

Benjamin Hager

unread,
Feb 24, 2016, 9:43:51 PM2/24/16
to web.py
Hello,

I have the following code that I initially wrote just using the BaseHTTPServer module:
def do_POST(self):
    """Respond to a POST request."""        
    body = "Message was Received!!"
    self.send_response(200, "OK")
    self.send_header("Content-type", "text/plain")
    self.send_header("Content-Length", str(len(body)))
    self.end_headers()
    self.wfile.write(body)

    self.data = self.file.readline()

I am trying to replicate the same functionality in Web.py but I seem to be having issues returning the successful response in the correct format. Here is what I have so far:

def POST(self):

        body = "Message was Received!!"

        web.ctx.status = "200 OK"

        web.header('Content-Type', 'text/plain')

        web.header('Content-Length', str(len(body)))

        web.ctx.output(body)


        data = web.data()


        return body


Any advice would be much appreciated! Thanks,

-Ben

Peter Buckner

unread,
Mar 28, 2016, 12:59:39 PM3/28/16
to web.py
Ben, you're working too hard

def POST(self):
   
return "Message wasReceived!!"

Is all you need to do. The rest is handled for you, though you can override it, if you like.

-Peter
Reply all
Reply to author
Forward
0 new messages