On 2019-08-13 15:49, tutor-requ...@python.org wrote:
> Send Tutor mailing list submissions to
>       tutor@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-requ...@python.org
> 
> You can reach the person managing the list at
>       tutor-ow...@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@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
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to