[Tutor] cgi module help (original poster)
rmlibre at riseup.net
rmlibre at riseup.net
Wed Aug 14 13:34:18 EDT 2019
On 2019-08-13 15:49, tutor-request at python.org wrote:
> Send Tutor mailing list submissions to
> tutor at python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
> https://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
> tutor-request at python.org
>
> You can reach the person managing the list at
> tutor-owner at python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
>
> Today's Topics:
>
> 1. Re: Fwd: Re: HELP PLEASE (Alan Gauld)
> 2. Re: HELP PLEASE (David L Neil)
> 3. Re: HELP PLEASE (Cameron Simpson)
> 4. Re: HELP PLEASE (Sithembewena L. Dube)
> 5. Re: HELP PLEASE (Alan Gauld)
> 6. Re: cgi module help (Peter Otten)
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> https://mail.python.org/mailman/listinfo/tutor
I went looking through the cgi module code and found the limitation is
in the way urllib's parse_qsl method parses data. It simply does not
handle nested dictionaries correctly. This lead to a simple
solution/hack by just json'ing the nested values:
"""Client Code"""
import json
from requests import sessions
inner_metadata = json.dumps({"date": "2019-08", "id": "0000"})
metadata = {"metadata": inner_metadata}
session = sessions.Session()
session.post(<url>, data=metadata)
This is received on the server side and can be parsed with:
"""Server Code"""
import cgi
import json
form = cgi.FieldStorage()
metadata_json = form.getvalue("metadata", None)
metadata = json.loads(metadata)
print(metadata)
> {"date": "2019-08", "id": "0000"}
print(type(metadata))
> dict
More information about the Tutor
mailing list