使用请求在 Python 中查询字符串数组参数 [英] Querystring Array Parameters in Python using Requests

查看:22
本文介绍了使用请求在 Python 中查询字符串数组参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图弄清楚如何使用 python-requests 发送一个请求,网址如下所示:

http://example.com/api/add.json?name='hello'&data[]='hello'&data[]='world'

通常我可以构建字典并执行以下操作:

data = {'name': 'hello', 'data': 'world'}response = requests.get('http://example.com/api/add.json', params=data)

这对我所做的大多数事情都很好.但是,我从上面点击了 url 结构,我不知道如何在没有手动构建字符串的情况下在 python 中做到这一点.我可以这样做,但我宁愿不这样做.

请求库中是否有我遗漏的内容或一些我不知道的 Python 功能?

另外,你甚至称这种类型的参数是什么,以便我可以更好地谷歌它?

解决方案

你所做的只是正确的.结果 url 与您期望的相同.

<预><代码>>>>有效载荷 = {'名称':'你好','数据':'你好'}>>>r = requests.get("http://example.com/api/params", params=payload)

你可以看到结果网址:

<预><代码>>>>打印(r.url)http://example.com/api/params?name=hello&data=hello

根据网址格式:

特别是,对查询字符串进行编码使用以下规则:

  • 字母(A–Z 和 a–z)、数字 (0–9) 和字符 .-~_ 保持原样
  • SPACE 编码为 +%20
  • 所有其他字符都编码为 %HH 十六进制表示,任何非 ASCII 字符首先编码为 UTF-8(或其他指定的编码)

所以array[]不会像预期的那样,会根据规则自动替换:

如果你建立一个像这样的网址:

`构建网址:http://example.com/api/add.json?name='hello'&data[]='hello'&data[]='world'`

输出将是:

<预><代码>>>>payload = {'name': 'hello', "data[]": 'hello','data[]':'world'}>>>r = requests.get("http://example.com/api/params", params=payload)>>>网址u'http://example.com/api/params?data%5B%5D=world&name=hello'

这是因为重复将被 url 中键的最后一个值替换,而 data[] 将被 data%5B%5D 替换.

如果 data%5B%5D 不是问题(如果服务器能够正确解析),那么你可以继续.

源链接

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='hello'&data[]='hello'&data[]='world'

Normally I can build a dictionary and do:

data = {'name': 'hello', 'data': 'world'}
response = requests.get('http://example.com/api/add.json', params=data)

That works fine for most everything that I do. However, I have hit the url structure from above, and I am not sure how to do that in python without manually building strings. I can do that, but would rather not.

Is there something in the requests library I am missing or some python feature I am unaware of?

Also what do you even call that type of parameter so I can better google it?

解决方案

What u are doing is correct only. The resultant url is same what u are expecting.

>>> payload = {'name': 'hello', 'data': 'hello'}
>>> r = requests.get("http://example.com/api/params", params=payload)

u can see the resultant url:

>>> print(r.url)
http://example.com/api/params?name=hello&data=hello

According to url format:

In particular, encoding the query string uses the following rules:

  • Letters (A–Z and a–z), numbers (0–9) and the characters .,-,~ and _ are left as-is
  • SPACE is encoded as + or %20
  • All other characters are encoded as %HH hex representation with any non-ASCII characters first encoded as UTF-8 (or other specified encoding)

So array[] will not be as expected and will be automatically replaced according to the rules:

If you build a url like :

`Build URL: http://example.com/api/add.json?name='hello'&data[]='hello'&data[]='world'`

OutPut will be:

>>> payload = {'name': 'hello', "data[]": 'hello','data[]':'world'}
>>> r = requests.get("http://example.com/api/params", params=payload)
>>> r.url
u'http://example.com/api/params?data%5B%5D=world&name=hello'

This is because Duplication will be replaced by the last value of the key in url and data[] will be replaced by data%5B%5D.

If data%5B%5D is not the problem(If server is able to parse it correctly),then u can go ahead with it.

Source Link

这篇关于使用请求在 Python 中查询字符串数组参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆