简体   繁体   中英

Python urllib2 sending POST data

I am attempting to send POST data using urllib, but the problem is normally you urlencode your dictionary of fields and then you send that along with your request. But some of my fields that I have to send to the server are named the exact same name, so using a dictionary I end up deleting a lot of my data out of the dictionary because you can't have two keys with the same name and different data.

I have my request data in a string format in the correct syntax(eg. var=value&var=value2&...) and I try to send that along with my urllib request, but I get a bad request every time. Is there any other way to urlencode the data before sending so I don't get a 400?

urllib.urlencode() can take a list of tuples, allowing you to specify multiple values for a parameter:

>>> urlencode([("var", "value"), ("var", "value2")])
'var=value&var=value2'

Or use a map with lists as values:

>>> p = {"var": ["value", "value2"], "var2": ["yetanothervalue"]}
>>> urlencode([(k, v) for k, vs in p.items() for v in vs])
'var=value&var=value2&var2=yetanothervalue'

You could even allow lists or strings as values, although it becomes less concise:

>>> p = {"var": ["value", "value2"], "var2": "yetanothervalue"}
>>> urlencode([(k, v) for k, vs in p.items()
...     for v in isinstance(vs, list) and vs or [vs]])
'var=value&var=value2&var2=yetanothervalue'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM