Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transferring dictionary via post request

Tags:

python

I am using Python and try to send a dictionary (that contains dynamic data) via HTTP-Post request to the server. What is the most efficient way to implement it?

like image 369
Nathan Avatar asked Nov 12 '10 09:11

Nathan


1 Answers

Use urllib.urlencode to encode the dictionary as a POST.

import urllib
import urllib2

mydict = {'key1': 'value1', 'key2': 'value2'}
encoded_dict = urllib.urlencode(mydict)

request = urllib2.Request(myurl, encoded_dict)
# now make the request
response = request.urlopen().read()
like image 83
Daniel Roseman Avatar answered Oct 18 '22 20:10

Daniel Roseman