当前位置:   article > 正文

python中应用requests库模拟postman请求携带token,使用get和post方法请求头携带token

请求头携带token

背景:

实际开发中,Python程序中需要调用后台接口,充当前端,后端规定请求头需要携带token

封装的get和post类:

  1. class RequestMethodCarryJson:
  2. """
  3. 定义请求类型
  4. 以json方式传递参数
  5. """
  6. def __init__(self):
  7. """初始化参数"""
  8. self.data = {}
  9. self.files = {}
  10. def get(self, url, data, headers):
  11. """
  12. 定义get方法请求
  13. :return:
  14. """
  15. try:
  16. return requests.get(url=url, data=data, headers=headers, timeout=60)
  17. except TimeoutError:
  18. return print('%s get request timeout!' % url)
  19. def getCarryToken(self, url, data, headers):
  20. """
  21. 定义get方法请求
  22. :return:
  23. """
  24. try:
  25. return requests.get(url=url, json=data, headers=headers, timeout=60)
  26. except TimeoutError:
  27. return print('%s get request timeout!' % url)
  28. def post(self, url, data, headers):
  29. """
  30. 定义post方法请求
  31. post携带token,看起来不需要像get那样添加一个getCarryToken特有的识别方法
  32. :return:
  33. """
  34. try:
  35. return requests.post(url=url, data=json.dumps(data), headers=headers, timeout=60)
  36. except TimeoutError:
  37. return print('%s post request timeout!' % url)
  1. class RequestMethodCarryFormData:
  2. """
  3. 定义请求类型
  4. 以表单方式form-data传递参数
  5. """
  6. def __init__(self):
  7. """初始化参数"""
  8. self.data = {}
  9. self.files = {}
  10. def get(self, url, data, headers):
  11. """
  12. 定义get方法请求
  13. :return:
  14. """
  15. try:
  16. return requests.get(url=url, data=data, headers=headers, timeout=60)
  17. except TimeoutError:
  18. return print('%s get request timeout!' % url)
  19. def getCarryToken(self, url, data, headers):
  20. """
  21. 定义get方法请求,额外添加token
  22. :return:
  23. """
  24. try:
  25. return requests.get(url=url, json=data, headers=headers, timeout=60)
  26. except TimeoutError:
  27. return print('%s get request timeout!' % url)
  28. def post(self, url, data, headers):
  29. """
  30. 定义post方法请求
  31. 这个携带json应该不需要额外改
  32. :return:
  33. """
  34. try:
  35. return requests.post(url=url, data=data, headers=headers, timeout=60)
  36. except TimeoutError:
  37. return print('%s post request timeout!' % url)

应用场景:

场景1——get请求时Headers携带token[传递参数以json格式]:

  1. ​token="里面填token内容"
  2. test1Info = test1(token)
  3. def test1(token):
  4. """
  5. 携带token,
  6. 访问平台已经存在的数据库,
  7. 以json格式传递数据
  8. :param token:
  9. :return:
  10. """
  11. url = "http://127.0.0.1:8088/backup/url1"
  12. headers = {'Content-Type': 'application/json;charset=utf-8', 'token': token}
  13. data = dict()
  14. data["param1"] = "param1"
  15. data["param2"] = "param2"
  16. resp = RequestMethodCarryJson().getCarryToken(url, data, headers).json()
  17. dbInfo = resp["items"]
  18. return dbInfo

场景2——post请求时Headers携带token[传递参数以application/x-www-form-urlencoded(表单)格式]:

  1. token="里面填token内容"
  2. test2Info = test2(token)
  3. def test2(token):
  4. url = "http://127.0.0.1:8088/testFormData/json/transfer"
  5. headers = {'Content-Type': 'application/x-www-form-urlencoded', 'token': token}
  6. data = dict()
  7. data["param1"] = "param1"
  8. """
  9. 这里不接受状态码
  10. """
  11. resp = RequestMethodCarryFormData().post(url, data, headers).json()
  12. info = resp["items"]
  13. if info == "格式正确":
  14. return True
  15. else:
  16. return False

场景3——踩坑后总结成功的代码:

  1. """
  2. 请求头携带token拿取信息:
  3. 1-post-以json格式传递数据,请求头携带token成功
  4. 形如:
  5. # resp = requests.post(url,json=data,headers=headers).json()
  6. resp = RequestMethodCarryJson().post(url, data, headers).json()
  7. 2-get-以json格式传递数据,请求头携带token成功
  8. # resp = requests.get(url,json=data,headers=headers).json()
  9. resp = RequestMethodCarryJson().getCarryToken(url, data, headers).json()
  10. :param platformInfo:
  11. :return:
  12. """

延伸理解:

相关资料参考:

postman中 form-data、x-www-form-urlencoded的区别_叫我峰兄的博客-CSDN博客

python requests 带请求头Token发起http请求_python request token_软件测试李同学的博客-CSDN博客

python发送requests请求时,使用登录的token值,作为下一个接口的请求头信息 - 大海一个人听 - 博客园 (cnblogs.com)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/37174
推荐阅读
相关标签
  

闽ICP备14008679号