1

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'
pinepain
12.9k3 gold badges66 silver badges67 bronze badges
asked Jul 8, 2014 at 9:15
1

1 Answer 1

1

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'}
answered Jul 8, 2014 at 9:32
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.