1

I'm trying to hit my HttpPost method in a .net core 3.1 application. When i try to hit it, the model object is created but none of the properties are set. The behaviour is same when i try to invoke using Swagger as well as postman. I tried to add a model as simple as :

public class Student{
public string Name{get; set;}
}

[HttpPost]
[Route("TestMethod")]
public void TestMethod(Student stu) {}

public void TestMethod(string name) {}

The strange behaviour is, if i pass it as query string, it works, but somehow it doesn't work with json (using postman). Also, if i create a demo app, it works perfectly fine, but not with my existing application

i used FromBody attribute as well.

Can someone help me with this?

Gurdeep
  • 25
  • 3
  • Can you share the raw request as well? You can get this from Postman as shown here https://stackoverflow.com/questions/33793629/postman-how-to-see-request-with-headers-and-body-data-with-variables-substitut – ekke Dec 30 '21 at 13:13

1 Answers1

1

you have to use this action

[HttpPost]
[Route("TestMethod")]
public IActionResult TestMethod([FromBody]Student stu) { return Ok(stu.Name)}

For a Post acton using Postman you have to select Body, raw, JSON in Postman menus. Your json should be

{ "Name":"Doe" }
Serge
  • 40,935
  • 4
  • 18
  • 45
  • Hi Serge, thanks for the advice, but it didn’t work. However I figured out that the problem was with middleware logging class. I was trying to read the request.Body into stream and it seemingly emptied the request body. I removed that code and now it works! – Gurdeep Dec 31 '21 at 14:30