之前只有requests发送过get请求,误以为post请求和get请求一摸一样,果不其然开始报错,一开始报错 请求https错误,加上参数:verify=False
res = requests.post(url=self.url, data=self.params['param'], verify=False, headers=headers)
请求头错误
{"timestamp":"2021-10-19T07:05:24.562+00:00","status":415,"error":"Unsupported Media Type","message":"","path":""}
后加上请求头信息
headers = {'Content-Type': 'application/json'}
又因为是请求https服务,所以会有报错提示,但是请求成功
InsecureRequestWarning: Unverified HTTPS request is being made to host '0.0.0.0'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html
后发现是一个警告,关闭该警告:
requests.packages.urllib3.disable_warnings()
然后就是持续的400错误,后联想了一下postman请求该接口的情景,发现请求参数不用携带参数名,需要将请求数据序列化
self.params = {'param': [1,2,3]}
requests.packages.urllib3.disable_warnings()
headers = {'Content-Type': 'application/json'}
body = json.dumps(self.params['param'])
res = requests.post(url=self.url, data=body, verify=False, headers=headers)
return res
|