Search code examples
pythonparsingcherrypy

Python cherrypy, get the query_string parsed


I'm trying to get the query and the data (GET params and the POST params) from a request

curl --data "foo=bar&hello=world" "http://localhost:8080/mypath?orange=5&apple=8"

.

query_string = cherrypy.request.query_string  # 'orange=5&apple=8'
post_data = cherrypy.request.body.params  # {'foo': 'bar', 'hello': 'world'}

The post_data is correctly dict formed. how can i parse the query_string like the post_data?

I was reading at cherrypy doc, and I was seeing this:

process_query_string()

Parse the query string into Python structures. (Core)

But this is not working, cherrypy.request.process_query_string() ais returning None

Any ideas?


Solution

  • CherryPy uses cherrypy.lib.httputil.parse_query_string for populating request.params with GET parameters, you can use it like this:

    from cherrypy.lib.httputil import parse_query_string
    parse_query_string(cherrypy.request.query_string)
    

    Which returns the dict with parsed query string parameters.