上下游传参

现在有两个接口

  1. 天气预报 — 返回城市的天气信息
  2. 发布话题 — 可以发布话题信息

在处理一些流程类的接口过程中, 有些接口之间的联系非常紧密,接口之间存在参数关联。

  1. 可以查询不同城市的天气信息
  2. 将查询出来的天气信息作为内容发布出来

在Postman中,做法如下:

  1. 创建天气预报接口

image.png
天气预报接口返回的结果

  1. {"status":0,"msg":"ok","result":{"city":"广州","cityid":75,"citycode":"101280101","date":"2021-06-07","week":"星期一","weather":"阴","temp":"30","temphigh":"34","templow":"25","img":"2","humidity":"68","pressure":"1001","windspeed":"3.5","winddirect":"东风","windpower":"1级","updatetime":"2021-06-07
  2. 14:15:00","index":[{"iname":"空调指数","ivalue":"部分时间开启","detail":"您将感到些燥热,建议您在适当的时候开启制冷空调来降低温度,以免中暑。"},{"iname":"运动指数","ivalue":"较不宜","detail":"有降水,推荐您在室内进行健身休闲运动;若坚持户外运动,须注意携带雨具并注意避雨防滑。"},{"iname":"紫外线指数","ivalue":"","detail":"紫外线强度较弱,建议出门前涂擦SPF12-15之间、PA+的防晒护肤品。"},{"iname":"感冒指数","ivalue":"少发","detail":"各项气象条件适宜,发生感冒机率较低。但请避免长期处于空调房间中,以防感冒。"},{"iname":"洗车指数","ivalue":"不宜","detail":"不宜洗车,未来24小时内有雨,如果在此期间洗车,雨水和路上的泥水可能会再次弄脏您的爱车。"},{"iname":"空气污染扩散指数","ivalue":"","detail":"气象条件有利于空气污染物稀释、扩散和清除,可在室外正常活动。"},{"iname":"穿衣指数","ivalue":"炎热","detail":"天气炎热,建议着短衫、短裙、短裤、薄型T恤衫等清凉夏季服装。"}],"aqi":{"so2":"8","so224":"0","no2":"17","no224":"0","co":"0.690","co24":"0.000","o3":"136","o38":"0","o324":"0","pm10":"35","pm1024":"0","pm2_5":"20","pm2_524":"0","iso2":"3","ino2":"9","ico":"7","io3":"45","io38":"0","ipm10":"35","ipm2_5":"29","aqi":"35","primarypollutant":"PM10","quality":"","timepoint":"2021-06-07
  3. 12:00:00","aqiinfo":{"level":"一级","color":"#00e400","affect":"空气质量令人满意,基本无空气污染","measure":"各类人群可正常活动"}},"daily":[{"date":"2021-06-07","week":"星期一","sunrise":"05:41","sunset":"19:11","night":{"weather":"雷阵雨","templow":"25","img":"4","winddirect":"持续无风向","windpower":"微风"},"day":{"weather":"雷阵雨","temphigh":"34","img":"4","winddirect":"持续无风向","windpower":"微风"}},{"date":"2021-06-08","week":"星期二","sunrise":"05:41","sunset":"19:11","night":{"weather":"中雨","templow":"25","img":"8","winddirect":"持续无风向","windpower":"微风"},"day":{"weather":"雷阵雨","temphigh":"34","img":"4","winddirect":"持续无风向","windpower":"微风"}},{"date":"2021-06-09","week":"星期三","sunrise":"05:41","sunset":"19:12","night":{"weather":"中雨","templow":"25","img":"8","winddirect":"持续无风向","windpower":"微风"},"day":{"weather":"中雨","temphigh":"32","img":"8","winddirect":"持续无风向","windpower":"微风"}},{"date":"2021-06-10","week":"星期四","sunrise":"05:41","sunset":"19:12","night":{"weather":"雷阵雨","templow":"26","img":"4","winddirect":"持续无风向","windpower":"微风"},"day":{"weather":"中雨","temphigh":"31","img":"8","winddirect":"持续无风向","windpower":"微风"}},{"date":"2021-06-11","week":"星期五","sunrise":"05:41","sunset":"19:12","night":{"weather":"雷阵雨","templow":"26","img":"4","winddirect":"持续无风向","windpower":"微风"},"day":{"weather":"雷阵雨","temphigh":"32","img":"4","winddirect":"持续无风向","windpower":"微风"}},{"date":"2021-06-12","week":"星期六","sunrise":"05:41","sunset":"19:13","night":{"weather":"雷阵雨","templow":"26","img":"4","winddirect":"持续无风向","windpower":"微风"},"day":{"weather":"雷阵雨","temphigh":"32","img":"4","winddirect":"持续无风向","windpower":"微风"}},{"date":"2021-06-13","week":"星期日","sunrise":"05:41","sunset":"19:13","night":{"weather":"雷阵雨","templow":"27","img":"4","winddirect":"持续无风向","windpower":"微风"},"day":{"weather":"雷阵雨","temphigh":"33","img":"4","winddirect":"持续无风向","windpower":"微风"}}],"hourly":[{"time":"14:00","weather":"","temp":"32","img":"2"},{"time":"15:00","weather":"","temp":"34","img":"2"},{"time":"16:00","weather":"","temp":"33","img":"2"},{"time":"17:00","weather":"","temp":"33","img":"2"},{"time":"18:00","weather":"","temp":"32","img":"2"},{"time":"19:00","weather":"","temp":"30","img":"0"},{"time":"20:00","weather":"","temp":"29","img":"0"},{"time":"21:00","weather":"","temp":"28","img":"0"},{"time":"22:00","weather":"","temp":"27","img":"0"},{"time":"23:00","weather":"多云","temp":"27","img":"1"},{"time":"0:00","weather":"","temp":"27","img":"0"},{"time":"1:00","weather":"","temp":"26","img":"0"},{"time":"2:00","weather":"","temp":"26","img":"0"},{"time":"3:00","weather":"雷阵雨","temp":"25","img":"4"},{"time":"4:00","weather":"多云","temp":"25","img":"1"},{"time":"5:00","weather":"雷阵雨","temp":"25","img":"4"},{"time":"6:00","weather":"","temp":"25","img":"2"},{"time":"7:00","weather":"","temp":"26","img":"2"},{"time":"8:00","weather":"","temp":"28","img":"2"},{"time":"9:00","weather":"","temp":"28","img":"2"},{"time":"10:00","weather":"","temp":"30","img":"2"},{"time":"11:00","weather":"","temp":"31","img":"2"},{"time":"12:00","weather":"","temp":"32","img":"2"},{"time":"13:00","weather":"","temp":"33","img":"2"}]}}

结果的值为标准的json 格式数据
在Postman中使用代码的方式 在天气预报接口上来设置变量
image.png

// 1. 获取服务器返回的结果
var res = pm.response.json()

// 2. 设置城市变量
pm.globals.set("city",res["result"]["city"])
// 3. 设置日期变量
pm.globals.set("date",res["result"]["date"])
// 4. 设置 week 变量
pm.globals.set("week",res["result"]["week"])

// 5.设置温度变量
pm.globals.set("temp",res["result"]["temp"])

从服务器返回的结果中将对应的值设置为变量,可以在Postman的变量预览面板中看到对应的值
image.png

  1. 在下一个接口中只需要通过调用变量的方式来传递这些参数即可。

image.png

{
    "accesstoken":"{{token}}",
    "title":"大家好,这里是{{city}}天气",
    "tab":"share",
    "content":"今天是{{date}},{{week}},现在的气温是{{temp}}"
}

总结:
处理上下游传参这一类型的接口问题,使用的方法主要就是设置变量的方式;

上面使用到的代码
上下游传参.postman_collection.json

数据驱动

  1. 创建数据文件, 文件格式使用csv文件格式

image.png
image.png
在postman的脚本中使用变量的方式 来引用值

{
    "accesstoken":"{{token}}",
    "title":"{{title}}",
    "tab":"{{tab}}",
    "content":"{{content}}"
}

image.png
要跟csv文件中定义的数据保持一致, 写完之后【保存】 http请求

3,点击【Run】运行按钮,在运行界面中选择 【Select File】
image.png
将第一步创建的csv文件上传到postman中

上传成功之后,点击【preview】 预览数据文件
image.png

可以看到定义的测试数据
image.png

选中 【save response】 点击【Run】
image.png
运行的时候会执行6次
image.png

数据驱动以及使用到的csv文件
数据驱动.postman_collection.json
testdata.csv

csv 文件中文乱码的解决办法

image.png
使用记事本 打开 csv文件,另存为
选择格式为 utf-8
image.png

复习录屏: https://www.bilibili.com/video/BV1Vh411D7Qn/