How to encode a MultiIDict in werkzeug 3.0

20 views Asked by At

I see url_encode no longer is available in Werkzeug 3.0.

Replacing it with urlencode from urllib.parse won't work with lists e.g.

>>> from werkzeug.datastructures import MultiDict
>>> from urllib.parse import urlencode
>>> d = MultiDict(('a', '1'), ('a', '2'))
>>> urlencode(d) #werkzeug url_encode returned 'a=1&a=2'
'a=1' 

How do I url encode a MultiDict in Werkzeug 3.0 so I can use it as x-www-form-urlencoded data?

1

There are 1 answers

0
neurino On

Found myself the not-so-intuitive-straightforward answer:

>>> from werkzeug.datastructures import MultiDict
>>> from urllib.parse import urlencode
>>> d = MultiDict(('a', '1'), ('a', '2'))
>>> urlencode(d.to_dict(flat=False), doseq=True)
'a=1&a=2'