Querystring Array Parameters in Python using Requests

后端 未结 4 1322
無奈伤痛
無奈伤痛 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 11:09

    Some api-servers expect json-array as value in the url query string. The requests params doesn't create json array as value for parameters.

    The way I fixed this on a similar problem was to use urllib.parse.urlencode to encode the query string, add it to the url and pass it to requests

    e.g.

    from urllib.parse import urlencode
    query_str = urlencode(params)
    url = "?" + query_str
    response = requests.get(url, params={}, headers=headers)
    

提交回复
热议问题