目录
具体问题具体分析,有些接口可以不带cookies直接请求,有些接口需要用户在登录的前提下才能运行,这就需要拿着cookies才能正常发送请求。
一般在接口测试过程中会通过post或者get的方式发送请求来得到需要的响应报文。
def post(url, data=None, json=None, **kwargs):
r"""Sends a POST request.
:param url: URL for the new :class:`Request` object.
:param data: (optional) Dictionary, list of tuples, bytes, or file-like
object to send in the body of the :class:`Request`.
:param json: (optional) json data to send in the body of the :class:`Request`.
:param \*\*kwargs: Optional arguments that ``request`` takes.
:return: :class:`Response <Response>` object
:rtype: requests.Response
"""
return request('post', url, data=data, json=json, **kwargs)
# User-Agent简称UA是一个特殊字符串头,是一种向访问网站提供你所使用的浏览器类型及版本、操作系统及版本、浏览器内核、等信息的标识。
headers = {"Accept": "*/*", "Content-Type": "application/json", "Accept-Encoding": "gzip, deflate, br","User-Agent": "python-requests/2.9.1"}
r = requests.post(base_url, data=json.dumps(payload), headers=headers, cookies=cookies)
def get(url, params=None, **kwargs):
r"""Sends a GET request.
:param url: URL for the new :class:`Request` object.
:param params: (optional) Dictionary, list of tuples or bytes to send
in the query string for the :class:`Request`.
:param \*\*kwargs: Optional arguments that ``request`` takes.
:return: :class:`Response <Response>` object
:rtype: requests.Response
"""
r = requests.get(url="https://www.douban.com/")
r = requests.get("https://movie.douban.com/subject_search?search_text=%E5%B0%8F%E8%B6%85&cat=1002")
pydata = {"search_text":"QQQ","cat":"1002"}
r = requests.get("https://movie.douban.com/subject_search",params=pydata)
r = requests.get(base_url, params=json.dumps(payload), headers=headers, cookies=cookies)
简洁描述起来就是cookies是存储在客户端的,能表明用户身份的一组信息。session是存储在服务器端的,一般为比较敏感的信息会以session的形式存储。
在Client-Server的交互中,为了服务器能准确的知道是谁在访问服务器,会为每个用户分配一个cookies,后续用户拿着cookies来发起各种请求服务器就能准确返回对应的内容。
联系本次接口框架中遇到的问题详述以下:
# -*- coding:utf-8 -*-
# author:Chang JinLing
# datetime:2021/2/24 18:11
import requests
import json
def login2():
headers = {"Accept": "*/*", "Content-Type": "application/json", "Accept-Encoding": "gzip, deflate, br","User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36",}
data = {"url": "settle-vue/bankInfoMainten/index.html"}
url = "http://xxxxxx/index.html"
r = requests.get(url=url, params=json.dumps(data), headers=headers)
print("r.headers:::", r.headers) # 通过该语句可以查看响应头中的所有信息
print("r.cookies:::", r.cookies) # 获取返回的cookies信息
print(r.cookies.get_dict()) # 获取返回的cookies信息,以字典类型输出
new_cookies = r.cookies.get_dict()
return new_cookies
if __name__ == "__main__":
login2()
# 执行该方法后可以看到能从该接口取到新生成的cookies,那么后续接口就可以调用该方法得到最新的cookies值。
# @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
# 执行结果:
E:\Private_Chang\project\python\Python3\installcontent\python.exe E:/Private_Chang/project/python/Pycharme/20201223new/20210225.py
r.headers::: {'Server': 'Apache-Coyote/1.1', 'Pragma': 'no-cache', 'Expires': 'Thu, 01 Jan 1970 00:00:00 GMT', 'Cache-Control': 'no-cache, no-store', 'Set-Cookie': 'JSESSIONID=CB82C793BC5D9CBD54943D325507F966; Path=/casserver; HttpOnly', 'Content-Type': 'text/html;charset=UTF-8', 'Transfer-Encoding': 'chunked', 'Date': 'Fri, 26 Feb 2021 11:48:42 GMT'}
r.cookies::: <RequestsCookieJar[<Cookie JSESSIONID=CB82C793BC5D9CBD54943D325507F966 for casserverd.local/casserver>]>
{'JSESSIONID': 'CB82C793BC5D9CBD54943D325507F966'}
type r.cookies.get_dict(): <class 'dict'>
Process finished with exit code 0
# -*- coding:utf-8 -*-
# author:Chang JinLing
# datetime:2021/2/24 11:54
import requests
import json
# 引入方法login2
from login2 import login2
class test_cookies():
def test_cookies_method(self):
headers = xxxxx
base_url = xxxxx
payload = xxxxx
cookies = login2()
r = requests.post(url=base_url, data=json.dumps(payload), headers=headers, cookies=cookies)
if __name__ == "__main__":
testcookies = test_cookies()
testcookies.test_cookies_method()