下面是一个示例,演示了如何使用ASP.NET Core Web API和PowerShell Invoke-RestMethod来进行POST请求,并获取返回的ResponseText、ReturnValue和ErrorMessage。
首先,创建一个ASP.NET Core Web API控制器,其中包含一个POST方法:
[Route("api/[controller]")]
[ApiController]
public class MyController : ControllerBase
{
[HttpPost]
public IActionResult Post([FromBody] MyModel model)
{
// 根据需要执行一些操作,并返回结果
if (model.Value == 1)
{
return Ok(new { ReturnValue = "Success", ResponseText = "操作成功" });
}
else
{
return BadRequest(new { ErrorMessage = "操作失败" });
}
}
}
public class MyModel
{
public int Value { get; set; }
}
然后,使用PowerShell的Invoke-RestMethod来发送POST请求:
$uri = "http://localhost:5000/api/my"
$data = @{
Value = 1
} | ConvertTo-Json
$response = Invoke-RestMethod -Uri $uri -Method Post -Body $data -ContentType "application/json"
# 获取返回的ResponseText、ReturnValue和ErrorMessage
$responseText = $response.ResponseText
$returnValue = $response.ReturnValue
$errorMessage = $response.ErrorMessage
Write-Host "Response Text: $responseText"
Write-Host "Return Value: $returnValue"
Write-Host "Error Message: $errorMessage"
在这个示例中,我们将POST请求发送到"http://localhost:5000/api/my",并将数据作为JSON格式的主体发送。
最后,我们从返回的响应中提取ResponseText、ReturnValue和ErrorMessage,并将它们打印出来。
请注意,此示例假定您已经启动了ASP.NET Core Web API应用程序,并且它正在监听"http://localhost:5000"上的请求。
上一篇:ASP.NET Core Web API和MongoDB与多个集合
下一篇:ASP.NET Core Web API和Swagger:即使字段不是必需的,也会出现IFormFile的必需字段错误。