1.发送JSON 请求
def post_orderBack(requestJson):
request_url = 'http://host:xxx'
headers = {'Content-Type': 'application/json;charset=UTF-8'}
res = requests.request("post", request_url, json=requestJson, headers=headers)
return res.status_code
python的requests的post请求中,有一个json参数。源码中对于此参数的说明如下:一个json序列化的python对象。python中字典表达与json很相似。
在post请求中,对传进来的json值,会做如下处理:
-
1、会使用json模块中的dumps方法转成json数据。
-
2、会增加消息头中的content_type为application/json
所以,json参数不需要提前使用json模块的方法转成json字符串。
请注意,这里有坑:如果在传参时,提前转换成json字符串:requests.request(“post”,url,json=json.dumps(a),headers=headers),
会出错!
遇到SSL 校验出错问题
需要加上一个参数
res = requests.request("post", request_url, json=requestJson, headers=headers,Verify=False)
本文详细解析了Python中使用requests库发送POST请求的方法,包括如何正确传递JSON数据,避免常见的错误,如预先转换JSON字符串的问题,以及如何处理SSL验证错误。
7168

被折叠的 条评论
为什么被折叠?



