(八)使用Postman工具调用WebAPI
访问WebAPI的方法,Postman工具比SoapUI好用一些。
1.不带参数的get请求
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
2.带参数的get请求
[HttpGet]
public WeatherForecast GetWeatherForecastById(int id)
{
return new WeatherForecast
{
Date = DateTime.Now.AddDays(id),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
};
}
3.post请求
[HttpPost]
public string PostWeatherForecast([FromBody] WeatherForecast message, [FromQuery] string param)
{
//定义json格式返回结果
var result = new { code = 0, city = param, data = message };
return JsonConvert.SerializeObject(result);
//return JsonSerializer.Serialize(result);
}
可以通过Body->form-data来传递两个参数:
也可以通过Params、Body->raw 分别传递两个参数: