How to parse empty value of parameter in HTTP request in python?
Asked Answered
H

1

7

I parse the following request param1=value1&param2=&param3=value3 using urllib.parse.parse_qs (python 3 wsgi application) but this function returns a dict with only param1 and param3 keys. What function can I use to get also the empty param2?

Homebody answered 20/10, 2014 at 19:59 Comment(0)
E
18

It's right there in the documentation for parse_qs():

The optional argument keep_blank_values is a flag indicating whether blank values in percent-encoded queries should be treated as blank strings. A true value indicates that blanks should be retained as blank strings. The default false value indicates that blank values are to be ignored and treated as if they were not included.

In other words, instead of

>>> parse_qs(querystring)
{'param1': ['value1'], 'param3': ['value3']}

... you need:

>>> parse_qs(querystring, keep_blank_values=True)
{'param2': [''], 'param1': ['value1'], 'param3': ['value3']}
Establishmentarian answered 21/10, 2014 at 1:47 Comment(1)
You are right. I have seen this parameter but I have though that it is about spaces which is not represented in percent-encoded form.Homebody

© 2022 - 2024 — McMap. All rights reserved.