Python: get post parameters from response object (requests)

In requests, response object has PreparedRequest object and it only contains ‘raw’ post request parameters (not as dict).

import requests
response ='', data={'foo':'test', 'bar':'test'})
# <PreparedRequest [POST]>
raw_params = response.request.body
# foo=test&bar=test # raw post parameters(not dict)

To parse the raw params to dict using urlparse.parse_qs method works, but it makes values to list despite a single value.

from urlparse import parse_qs
params = parse_qs(raw_params)
# {'bar': ['test'], 'foo': ['test']
# ['test']  # values are list

In Django, using django.http.QueryDict works fine!

from django.http import QueryDict
params = QueryDict(raw_params)
print params['foo']
# test  # not list fine!

Leave a Reply

Your email address will not be published. Required fields are marked *