Home > Software design >  PUT method not working in Postman 400 Bad Request
PUT method not working in Postman 400 Bad Request

Time:07-31

In Postman, I get "The request cannot be fulfilled due to bad syntax."

My API runs when I go to test it but I keep getting a 400 error when sending the PUT in Postman.

    [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();
        }

CodePudding user response:

have to fix action, don't use status twice, and add frombody attribute. Also check Content-Type in Postman, it should be '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

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

CodePudding user response:

I suspect you need to change Content-Type for your request in Postman.

Content-Type comes to the server as a header. In Postman it is set:

  1. via the Headers tab OR
  2. automatically, if you select the appropriate type in the Body Tab.

Most likely the value you need is 'application/json'.

  •  Tags:  
  • c#
  • Related