Reputation: 17
How can I send an html array with python requests lib?
There is CURL request example which I want realize with requests lib API:
curl <some url there> -H <some header there> --data 'path[]=/empty&path[]=/Burns.docx&path[]=/Byron.doc'
Upvotes: 1
Views: 546
Reputation: 1124518
The params
keyword takes a dictionary representing query parameters; if you give a key a list of values, then each of those values is represented as a separate entry in the query string. The []
suffix on names is a PHP / Ruby-on-Rails convention, you'll need to supply it yourself:
params = {'path[]': ['/empty', '/Burns.docx', '/Byron.doc']}
headers = {'Some-header', 'Header value'}
response = requests.get(url, params=params, headers=headers)
Here the path[]
parameter is given a list of values, each becomes a separate path[]=<value>
entry in the query string.
Demo:
>>> import requests
>>> params = {'path[]': ['/empty', '/Burns.docx', '/Byron.doc']}
>>> url = 'http://httpbin.org/get'
>>> response = requests.get(url, params=params)
>>> response.url
u'http://httpbin.org/get?path%5B%5D=%2Fempty&path%5B%5D=%2FBurns.docx&path%5B%5D=%2FByron.doc'
>>> from pprint import pprint
>>> pprint(response.json())
{u'args': {u'path[]': [u'/empty', u'/Burns.docx', u'/Byron.doc']},
u'headers': {u'Accept': u'*/*',
u'Accept-Encoding': u'gzip, deflate, compress',
u'Connection': u'close',
u'Host': u'httpbin.org',
u'User-Agent': u'python-requests/2.2.1 CPython/2.7.6 Darwin/13.2.0',
u'X-Request-Id': u'3e4c8341-3da9-4a26-9527-f983904b3b18'},
u'origin': u'84.92.98.170',
u'url': u'http://httpbin.org/get?path[]=%2Fempty&path[]=%2FBurns.docx&path[]=%2FByron.doc'}
Upvotes: 1