当前位置:首页 > 编程笔记 > 正文
已解决

postman-pre-request-scripts使用

来自网友在路上 170870提问 提问时间:2023-09-27 12:58:18阅读次数: 70

最佳答案 问答题库708位专家为你答疑解惑

一、场景

二、定义模拟接口

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SaaS.Framework.DataTransfer;
using System.Threading.Tasks;namespace SaaS.KDemo.Api.Controllers
{[Route("api/[controller]")][ApiController]public class AuthPostmanController : ControllerBase{[AllowAnonymous][HttpPost("getAuth")][ProducesResponseType(typeof(OkResponse), StatusCodes.Status200OK)]public virtual async Task<IActionResult> GetAuth([FromBody] InputPar inputPar){return Ok(new SucceedResponse<object>() { Data = new { SecreteKey = inputPar.ShopID + "_TQtNDQ0Mi00NThmL", SecreteToken = inputPar.ShopID + "_YTk2ZGUzOTQtNDQ0Mi00NThmLTgzMzktM2M3YmYyNGEwYjY0" } });}[AllowAnonymous][HttpGet("getData")][ProducesResponseType(typeof(OkResponse), StatusCodes.Status200OK)]public virtual async Task<IActionResult> GetData(string  name,string secreteKey,string secreteToken){return Ok(new SucceedResponse<object>() { });}}public class InputPar{public int ShopID { get; set; }}
}

三、Postman

设置环境变量

配置pre-request-scripts

var shopID= parseInt(pm.environment.get('ShopID')) ;
console.log(shopID)if(pm.request.method=="POST"){var raw=JSON.parse(pm.request.body.raw)console.log(raw)for(var key in raw){console.log(key+':'+raw[key])}}else{var queryParam = pm.request.url.query.membersconsole.log(queryParam)for (var item in queryParam) {if(queryParam[item].disabled){continue;}console.log(queryParam[item].key+':'+queryParam[item])}}
var url="http://localhost:5901/api/AuthPostman/getAuth";var getAuth = {url: url,header: {'content-type': 'application/json-patch+json'},method:"POST",body:{mode:"raw",raw:JSON.stringify({ shopID:shopID})}
};pm.sendRequest(getAuth,function (err, response) {var res=response.json();console.log(res);if(res.succeed){pm.collectionVariables.set("secreteKey", res.data.secreteKey);  pm.collectionVariables.set("secreteToken", res.data.secreteToken);  }
});

查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"postman-pre-request-scripts使用":http://eshow365.cn/6-14674-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!