简体   繁体   English

PUT 方法在 Postman 400 错误请求中不起作用

[英]PUT method not working in Postman 400 Bad Request

In Postman, I get "The request cannot be fulfilled due to bad syntax."在 Postman 中,我收到“由于语法错误,无法完成请求”。

My API runs when I go to test it but I keep getting a 400 error when sending the PUT in Postman.我的 API 在我 go 测试它时运行,但是在 Postman 中发送 PUT 时我一直收到 400 错误。

    [HttpPut("{status}")]
    [Route ("status")]
    public async Task<IActionResult> UpdateIntervention(string status, Intervention intervention)
    {
        try
        {
            await _context.SaveChangesAsync();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (intervention.status == "Pending")
        {

            intervention.status = "InProgress";
            intervention.start_date = DateTime.Now;
            intervention.result = "Begun Maintenance";
            _context.interventions.Update(intervention);
            
        } else if (intervention.status == "InProgress") {

            intervention.status = "Completed";
            intervention.start_date = DateTime.Now;
            intervention.result = "Problem Solved";
            _context.interventions.Update(intervention);
        }
        await _context.SaveChangesAsync();
        }
          

        return NoContent();
        }

have to fix action, don't use status twice, and add frombody attribute.必须修复动作,不要使用两次状态,并添加 frombody 属性。 Also check Content-Type in Postman, it should be 'application/json'.还要检查 Postman 中的 Content-Type,它应该是 'application/json'。

[HttpPut("{status}")]
public async Task<IActionResult> UpdateIntervention(string status,[FromBody] Intervention intervention)

//or 
[HttpPut]
[Route("{status}")]
public async Task<IActionResult> UpdateIntervention(string status,[FromBody] Intervention intervention)

but it is better to use the a full route to avoid overlaping但最好使用完整的路线以避免重叠

[HttpPut("~/..controller name../UpdateIntervention/{status}")]
public async Task<IActionResult> UpdateIntervention(string status,[FromBody] Intervention intervention)

in this case your url should be too在这种情况下,您的 url 也应该是

http.../..controller name../UpdateIntervention/{status}

I suspect you need to change Content-Type for your request in Postman.我怀疑您需要在 Postman 中为您的请求更改Content-Type

Content-Type comes to the server as a header. Content-Type作为 header 进入服务器。 In Postman it is set:在 Postman 中设置:

  1. via the Headers tab OR通过标题选项卡或
  2. automatically, if you select the appropriate type in the Body Tab.自动,如果您 select 在Body选项卡中选择适当的类型。

Most likely the value you need is 'application/json'.您需要的值很可能是“application/json”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM