mirror of https://github.com/scrapy/scrapy.git
FormRequest: urlencode multiples values of a single key using doseq
this prevents urllib.urlencode from sending the repr of the value when it founds a list or tuple. --HG-- extra : convert_revision : svn%3Ab85faa78-f9eb-468e-a121-7cced6da292c%40825
This commit is contained in:
parent
6650e56354
commit
fe2f018b1a
|
|
@ -10,13 +10,22 @@ import urllib
|
|||
from scrapy.http.request import Request
|
||||
from scrapy.utils.python import unicode_to_str
|
||||
|
||||
def _unicode_to_str(string, encoding):
|
||||
if hasattr(string, '__iter__'):
|
||||
return [unicode_to_str(k, encoding) for k in string]
|
||||
else:
|
||||
return unicode_to_str(string, encoding)
|
||||
|
||||
|
||||
class FormRequest(Request):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
formdata = kwargs.pop('formdata', None)
|
||||
Request.__init__(self, *args, **kwargs)
|
||||
|
||||
if formdata:
|
||||
items = formdata.iteritems() if isinstance(formdata, dict) else formdata
|
||||
query = [(unicode_to_str(k, self.encoding), unicode_to_str(v, self.encoding)) for k, v in items]
|
||||
self.body = urllib.urlencode(query)
|
||||
query = [(unicode_to_str(k, self.encoding), _unicode_to_str(v, self.encoding))
|
||||
for k, v in items]
|
||||
self.body = urllib.urlencode(query, doseq=1)
|
||||
self.headers['Content-Type'] = 'application/x-www-form-urlencoded'
|
||||
|
|
|
|||
|
|
@ -174,5 +174,10 @@ class RequestTest(unittest.TestCase):
|
|||
self.assertEqual(r3.encoding, 'latin1')
|
||||
self.assertEqual(r3.body, 'price=%A3+100')
|
||||
|
||||
# using multiples values for a single key
|
||||
data = {'price': u'\xa3 100', 'colours': ['red', 'blue', 'green']}
|
||||
r3 = FormRequest("http://www.example.com", formdata=data)
|
||||
self.assertEqual(r3.body, 'colours=red&colours=blue&colours=green&price=%C2%A3+100')
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
|
|||
Loading…
Reference in New Issue