postman请求不到返回体_POSTMAN POST请求返回不支持的媒体类型

在尝试使用Postman测试ASP.NET Core MVC 2 API的POST方法时,遇到了415错误,提示'不支持的媒体类型'。问题出现在Content-Type设置和请求体的数据格式上。虽然PowerShell的Invoke-RestMethod能正常工作,但在Postman中,设置Content-Type为application/json,并尝试使用表单数据或原始JSON数据时,请求失败。解决方案可能涉及正确配置请求头和请求体的内容。
摘要由CSDN通过智能技术生成

我正在遵循Adam Freeman的“Pro ASP.NET Core MVC 2”的API说明 . 我有以下API控制器类:

[Route("api/[controller]")]

public class ReservationController : Controller

{

private IRepository repository;

public ReservationController(IRepository repo) => repository = repo;

[HttpGet]

public IEnumerable Get() => repository.Reservations;

[HttpGet("{id}")]

public Reservation Get(int id) => repository[id];

[HttpPost]

public Reservation Post([FromBody] Reservation res) =>

repository.AddReservation(new Reservation

{

ClientName = res.ClientName,

Location = res.Location

});

[HttpPut]

public Reservation Put([FromBody] Reservation res) => repository.UpdateReservation(res);

[HttpPatch("{id}")]

public StatusCodeResult Patch(int id, [FromBody]JsonPatchDocument patch)

{

Reservation res = Get(id);

if(res != null)

{

patch.ApplyTo(res);

return Ok();

}

return NotFound();

}

[HttpDelete("{id}")]

public void Delete(int id) => repository.DeleteReservation(id);

}

该文本使用PowerShell来测试API,但我想使用Postman . 在Postman中,GET调用有效 . 但是,我无法让POST方法返回值 . 错误显示为“状态代码:415;不支持的媒体类型'

在Postman中,Body使用表单数据,其中:

key: ClientName, value: Anne

key: Location, value: Meeting Room 4

如果我选择类型下拉列表为“JSON”,则会显示“Unexpected'S”

在 Headers 中,我有:

`key: Content-Type, value: application/json`

我还在体内尝试了以下原始数据,而不是表单数据:

{clientName="Anne"; location="Meeting Room 4"}

使用PowerShell时,API控制器可以正常工作并返回正确的值 . 对于POST方法,以下工作:

Invoke-RestMethod http://localhost:7000/api/reservation -Method POST -Body (@{clientName="Anne"; location="Meeting Room 4"} | ConvertTo-Json) -ContentType "application/json"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值