• 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. });

  • 相关阅读:
    Ubuntu在终端中打开QtCreator
    新库上线 | CnOpenData国际货运代理信息数据
    sql 统计新客户
    基于JAVA+SpringBoot+UniApp+Vue的前后端分离的手机移动端图书借阅平台
    校园网WiFi IPv6免流上网
    服务器数据恢复-Xen server虚拟机数据恢复案例
    openGauss学习笔记-118 openGauss 数据库管理-设置数据库审计-维护审计日志
    可扩展性对物联网管理系统有哪些影响?
    JSON(及其在ajax前后端交互的过程)小识
    PostgreSQL VACUUM 之深入浅出 (一)
  • 原文地址:https://blog.csdn.net/xiaoxionglove/article/details/133203279