一、场景
二、定义模拟接口
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.members
console.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);
}
});