如何使用postman及插件postman interceptor 实现模拟带站点cookie的请求调用

前提:浏览器chrome及chrome插件安装

背景:前后端分离后,调试后端接口需要模拟前端发起的请求,构建请求参数较为费事,不如将浏览器cookie及请求参数一起带出通过postman 模拟前端请求。

1、安装chrome插件postman interceptor及谷歌应用postman

请在谷歌应用商店下载postman interceptor插件并安装;

具体步骤:先到谷歌网上商店下载插件postman interceptor;或者在postman官网导向到插件地址https://www.getpostman.com/apps点击【Get Interceptor(Free)】将会导向到谷歌商店上 postman interceptor地址。

安装插件步骤:chrome浏览器右上角【自定义及控制】按钮()=》【更多工具】=》【扩展程序】,将下载的插件拖入 chrome://extensions/  页面中,安装好的postman interceptor插件如下

如果不能正常访问谷歌网上商店,请先下载并安装【谷歌访问助手】,具体步骤请移步参考:https://blog.csdn.net/longyc2010/article/details/63685501

安装好后,再在谷歌商店下载安装postman,https://chrome.google.com/webstore/detail/fhbjgbiflinjbdggehcddcbncdddomop?utm_source=chrome-ntp-launcher ,成功后如下:

2、如何使用postman及postman interceptor 模拟前端请求向后端接口发起请求

步骤:打开interceptor。默认是 off 如下图所示,打开变为on即可。filter 过滤请求路径匹配。* 即全部放行。

再打开 chrome 应用 postman。 比如mac中 【command+空格键】打开聚焦搜索 postman

postman 启动后打开上面的 interceptor ,

 再在左侧会看到 history标签下 当前浏览器请求的url。如下图所示:

ok,假如现在 你已启动本地代码项目正在等待接受前端的请求,但是请求需要登录后cookie;你现在所需要做的就是正常登录比如测试环境项目后,定位到你要调试的请求所在页面,在postman左侧history中会跟踪到相关的请求url,点击进去即可看到相应请求头、body等参数。如下图:重点是将请求的url改为本地dev域名请求(域名可做host映射为本地127.0.0.1 devxx.xx.com).

Boy,Good Luck!!

附:

Postman Interceptor的主要功能:

  • 记录浏览器请求并直接导入到Postman
  • 可添加Filter,对浏览器中的请求进行过滤

Postman Intercepter使用方法:

1.在Postman工具中打开postman Intercepter功能 
2.如果只需要测试特定网站,可以在 postman interceptor插件中设置 Filter requests。 
Filter requests默认是“.*”,表示捕获所有的网站。如果只想要捕获百度,可以在Filter requests里输入“baidu”。

3.在chrome中进行操作,比如打开百度,然后搜索关键字“hello”,然后点击“百度一下”。 
4.打开 postman interceptor。可以看到 “last 10 requests“。 
5.打开Postman,可以看到“History”显示chrome中进行的操作。 
6.然后就可以在Postman对 request进行修改,过滤,以及编写测试。

Postman Interceptor,可以让postman中发送请求的时候使用这个网站的浏览器cookie。发送请求的时候带上了自己的cookie,如果在尝试获取数据时不能访问,可以考虑Postman Interceptor。

猜你喜欢

转载自blog.csdn.net/manbufenglin/article/details/82759642