• postman-pre-request-scripts使用


    一、场景

    二、定义模拟接口

    1. using Microsoft.AspNetCore.Authorization;
    2. using Microsoft.AspNetCore.Http;
    3. using Microsoft.AspNetCore.Mvc;
    4. using SaaS.Framework.DataTransfer;
    5. using System.Threading.Tasks;
    6. namespace SaaS.KDemo.Api.Controllers
    7. {
    8. [Route("api/[controller]")]
    9. [ApiController]
    10. public class AuthPostmanController : ControllerBase
    11. {
    12. [AllowAnonymous]
    13. [HttpPost("getAuth")]
    14. [ProducesResponseType(typeof(OkResponse), StatusCodes.Status200OK)]
    15. public virtual async Task<IActionResult> GetAuth([FromBody] InputPar inputPar)
    16. {
    17. return Ok(new SucceedResponse<object>() { Data = new { SecreteKey = inputPar.ShopID + "_TQtNDQ0Mi00NThmL", SecreteToken = inputPar.ShopID + "_YTk2ZGUzOTQtNDQ0Mi00NThmLTgzMzktM2M3YmYyNGEwYjY0" } });
    18. }
    19. [AllowAnonymous]
    20. [HttpGet("getData")]
    21. [ProducesResponseType(typeof(OkResponse), StatusCodes.Status200OK)]
    22. public virtual async Task<IActionResult> GetData(string name,string secreteKey,string secreteToken)
    23. {
    24. return Ok(new SucceedResponse<object>() { });
    25. }
    26. }
    27. public class InputPar
    28. {
    29. public int ShopID { get; set; }
    30. }
    31. }

    三、Postman

    设置环境变量

    配置pre-request-scripts

    1. var shopID= parseInt(pm.environment.get('ShopID')) ;
    2. console.log(shopID)
    3. if(pm.request.method=="POST")
    4. {
    5. var raw=JSON.parse(pm.request.body.raw)
    6. console.log(raw)
    7. for(var key in raw)
    8. {
    9. console.log(key+':'+raw[key])
    10. }
    11. }
    12. else
    13. {
    14. var queryParam = pm.request.url.query.members
    15. console.log(queryParam)
    16. for (var item in queryParam) {
    17. if(queryParam[item].disabled)
    18. {
    19. continue;
    20. }
    21. console.log(queryParam[item].key+':'+queryParam[item])
    22. }
    23. }
    24. var url="http://localhost:5901/api/AuthPostman/getAuth";
    25. var getAuth = {
    26. url: url,
    27. header: {
    28. 'content-type': 'application/json-patch+json'
    29. },
    30. method:"POST",
    31. body:{
    32. mode:"raw",
    33. raw:JSON.stringify({ shopID:shopID})
    34. }
    35. };
    36. pm.sendRequest(getAuth,function (err, response) {
    37. var res=response.json();
    38. console.log(res);
    39. if(res.succeed)
    40. {
    41. pm.collectionVariables.set("secreteKey", res.data.secreteKey);
    42. pm.collectionVariables.set("secreteToken", res.data.secreteToken);
    43. }
    44. });

  • 相关阅读:
    数据查询优化技术方案
    Redis 集群偶数节点跨地域部署之高可用测试
    1019 数字黑洞
    DEJA_VU3D - Cesium功能集 之 057-百度地图纠偏
    软件测试/测试开发丨Web自动化—capability参数配置 学习笔记
    自动驾驶的法律和伦理问题
    一文学会如何使用原型模式
    装饰模式(Decorator)
    C语言程序设计算法题 -- lab07(1027 - 1030)
    本地FTP YUM源报错处理
  • 原文地址:https://blog.csdn.net/xiaoxionglove/article/details/133203279