2

I am new to cherrypy and couldn't find proper doc abt the topic in question.

How can I handle POST body request in Cherrypy ?

NOTE : I have used mod-python. In it req.read() directly gives content of in-body post content, sent like -

curl -X POST -d @test.xml "http://127.0.0.1:80/generate/gen.py"

Here test.xml is file containing xml content.

I want to use cherrypy only ... please don't suggest to use mod-python :P

user2952821
  • 743
  • 2
  • 6
  • 19
  • Do a research before asking a question. Official CherryPy [file upload tutorial](https://bitbucket.org/cherrypy/cherrypy/src/default/cherrypy/tutorial/tut09_files.py). SO questions on handling [multipart](http://stackoverflow.com/q/13002676/2072035) and [non-multipart](http://stackoverflow.com/q/26576349/2072035) uploads. – saaj May 04 '15 at 13:27
  • @saaj That's not an upload, that request will POST the content of the test.xml in the body. You can find the documentation for curl online via google, etc. – The Tahaan May 04 '15 at 15:34
  • An upload is an informal name for the process of transferring a local file to a remote host via HTTP. In your case it is indeed an upload. Curl sends POST request with raw file contents as `application/x-www-form-urlencoded` MIME, which is a misnomer because it's in fact a `application/octet-stream`. So either provide request content type yourself and follow *non-multipart* link, or use a client that respects standards and follow the other. – saaj May 04 '15 at 16:26

1 Answers1

2

You can use the cherrypy.request.body.read() method to obtain the XML. For example:

class MyApp(object):

    @cherrypy.expose
    def my_handler(self):
        body = cherrypy.request.body.read()
        # process XML from body here...
        return "I got %s bytes." % len(body)
The Tahaan
  • 6,915
  • 4
  • 34
  • 54