Querystring Array Parameters in Python using Requests

后端 未结 4 1331
無奈伤痛
無奈伤痛 2020-12-10 10:10

I have been trying to figure out how to use python-requests to send a request that the url looks like:

http://example.com/api/add.json?name=\'he         


        
4条回答
  •  不知归路
    2020-12-10 10:59

    One solution if using the requests module is not compulsory, is using the urllib/urllib2 combination:

    payload = [('name', 'hello'), ('data[]', ('hello', 'world'))]
    params = urllib.urlencode(payload, doseq=True)
    sampleRequest = urllib2.Request('http://example.com/api/add.json?' + params)
    response = urllib2.urlopen(sampleRequest)
    

    Its a little more verbose and uses the doseq(uence) trick to encode the url parameters but I had used it when I did not know about the requests module.

    For the requests module the answer provided by @Tomer should work.

提交回复
热议问题