今天写登录的时候,用postman请求是正常的,用python写的请求返回服务器异常,最后找到的原因是data数据传过去的默认是字符串
import requests
class HttpRequest:
def http_request(self, method, url, data=None, header = None, cookie=None):
if method.lower() == 'get':
res = requests.get(url,data, headers = header, cookies = cookie, verify = False)
elif method.lower() == 'post':
res = requests.post(url, data, headers = header, cookies = cookie, verify = False)
elif method.lower() == 'put':
res = requests.post(url, data, headers = header, cookies = cookie, verify = False)
else:
print("没有此方法,请联系管理员")
return
return res
if __name__ == '__main__':
method = 'post'
url = 'https:www.account/v2/session/loginByOne?corpId=8GUafWTqYo0o00'
data = {"fullPhone":"86-17610296533","password":"123456"}
header = {'Content-Type': 'application/json'}
res = HttpRequest().http_request(method, url, data, header)
print(res)
需要将data修改成json修改代码如下
import requests
class HttpRequest:
def http_request(self, method, url, data=None, header = None, cookie=None):
if method.lower() == 'get':
res = requests.get(url, json = data, headers = header, cookies = cookie, verify = False)
elif method.lower() == 'post':
res = requests.post(url, json = data, headers = header, cookies = cookie, verify = False)
elif method.lower() == 'put':
res = requests.post(url, json = data, headers = header, cookies = cookie, verify = False)
else:
print("没有此方法,请联系管理员")
return
return res
if __name__ == '__main__':
method = 'post'
url = 'https://account/v2/session/loginByOne?corpId=8GUafWTqYo0o00'
data = {"fullPhone":"86-17610296533","password":"123456"}
header = {'Content-Type': 'application/json'}
res = HttpRequest().http_request(method, url, data, header)
print(res)
|