![](/img/trans.png)
[英]How to add a backlash (\) at the beginning and the end of a string in Python
[英]Python Requests - add text at the beginning of query string
通过python请求GET请求发送数据时,我需要在查询字符串的开头专门添加一些内容。 我尝试通过dict和json字符串传递数据,但是没有运气。
由请求产生时出现的请求:
/apply/.../explain?%7B%22......
由他们的交互式API文档(Swagger)生成时显示的请求:
/apply/.../explain?record=%7B%22....
我的数据的键值对遵循上面摘录的位置。
最终,我认为缺少的部分是他们的文档产生的record = 。 这是唯一与请求产生的内容不同的部分。
目前,我已经建立了这样的东西:
import requests
s = requests.Session()
s.auth = requests.auth.HTTPBasicAuth(username,password)
s.verify = certificate_path
# with data below being a dictionary of the values I need to pass.
r = s.get(url,data=data)
我正尝试在下面包含文档的图像,但是还没有足够的声誉:
“ GET”请求没有数据,该数据用于“ POST”和朋友。
您可以改为使用params
kwarg发送查询字符串参数:
>>> params = {'record': '{"'}
>>> response = requests.get('http://www.example.com/explain', params=params)
>>> response.request.url
'http://www.example.com/explain?record=%7B%22'
从评论中,我觉得有必要对此进行解释。
http://example.com/sth?key=value&anotherkey=anothervalue
假设您有一个类似上述的网址,以便只用编写的python请求进行调用
response = requests.get('http://example.com/sth', params={
'key':'value',
'anotherkey':'anothervalue'
})
请记住,如果您的值或键中有任何特殊字符,它们将被转义,这就是问题中URL的%7B%2
部分的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.