How to send a POST request using django?
In Python 2, a combination of methods from urllib2
and urllib
will do the trick. Here is how I post data using the two:
post_data = [('name','Gladys'),] # a sequence of two element tuples
result = urllib2.urlopen('http://example.com', urllib.urlencode(post_data))
content = result.read()
urlopen() is a method you use for opening urls. urlencode() converts the arguments to percent-encoded string.
Here's how you'd write the accepted answer's example using python-requests
:
post_data = {'name': 'Gladys'}
response = requests.post('http://example.com', data=post_data)
content = response.content
Much more intuitive. See the Quickstart for more simple examples.