Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cherrypy.request.body.read() error

I'm having some problems accessing http requests' bodies with the CherryPy framework. I'm using CherryPy 3.2.0 on a x86_64 Arch Linux machine w/ Python3 and Aptana Web Studio IDE.

When I try to access a request's body via the usual cherrypy.request.body.read(), i get the error

File "/usr/lib/python3.2/site-packages/cherrypy/_cpreqbody.py", line 450, in read
return self.fp.read(size, fp_out)
TypeError: read() takes at most 2 positional arguments (3 given)

The code causing the error is:

import cherrypy

class Test:
    def index(self):
        print(cherrypy.request.body.read())
        #print(cherrypy.request.body.readline()) <- this works!
    return 'HelloWorld'
index.exposed = True

if __name__ == '__main__':
    cherrypy.quickstart(Test())

However, using

cherrypy.request.body.readline() or cherrypy.request.body.readlines(n)

instead of

cherrypy.request.body.read()

i can skim through the request's body just fine. I tried googling for a solution but found none. Considering i'm a total python newbie, there must be something i am doing wrong, but what?

Thanks in advance for your precious help.

like image 867
Jacoscaz Avatar asked Nov 02 '11 15:11

Jacoscaz


1 Answers

The body.read() method works properly only if the request body was processed, which happens only when request.process_request_body is True (it is by default) and when the request method is in request.method_with_bodies which is only PUT and POST by default, but not GET (which you probably used when requesting the page with a browser).

like image 124
Cito Avatar answered Sep 30 '22 06:09

Cito