Python: urllib2.urlopen(url, data) Why do you have to urllib.urlencode() the data?
Asked Answered
B

2

7

I thought that a post sent all the information in HTTP headers when you used post (I'm not well informed on this subject obviously), so I'm confused why you have to urlencode() the data to a key=value&key2=value2 format. How does that formatting come into play when using POST?:

# Fail
data = {'name': 'John Smith'}
urllib2.urlopen(foo_url, data)

but

# Success
data = {'name': 'John Smith'}
data = urllib.urlencode(data)
urllib2.urlopen(foo_url, data)
Bartle answered 3/2, 2010 at 15:20 Comment(0)
G
9

It is related to the "Content-Type" header: the client must have an idea of how the POST data is encoded or else how would it know how to decode it?

The standard way of doing this is through application/x-www-form-urlencoded encoding format.

Now, if the question is "why do we need to encode?", the answer is "because we need to be able to delineate the payload in the HTTP container".

Gibson answered 3/2, 2010 at 15:27 Comment(0)
S
2

Data must be in the standard application/x-www-form-urlencoded format. urlencode converts your args to a url-encoded string.

Sivia answered 3/2, 2010 at 15:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.