cherrypy.request.body.read() error
Asked Answered
Y

1

5

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.

Yezd answered 2/11, 2011 at 15:3 Comment(1)
What does print(type(cherrypy.request.body.fp)) give you? (Just before the print line.)Wernerwernerite
I
10

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).

Inflated answered 2/11, 2011 at 15:42 Comment(1)
Thanks for this explanation! The exception thrown must be one of the least descriptive I have ever seen...Appleton

© 2022 - 2024 — McMap. All rights reserved.