(七)使用SoapUI工具调用WebAPI
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();
}
1)打开SoapUI工具,新建REST Project
2)输入请求地址:
3)选择GET方式,确认请求地址ok的话,点执行按钮发送请求,返回服务端的响应结果。
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);
}
修改方法名,添加参数,点执行按钮发送请求,返回服务端的响应结果。