3

I created a WebAPI with Visual Studio 2017 using its .Net Core template. I am able to test my Get() code with Postman and everything works correctly. Now, I would like to send a Post request so it calls the following code.

[HttpPost]
public void Post([FromBody] string value)
{
  Console.WriteLine("value" + value);
}

However, I get the following error when call

https://localhost:44364/api/carbon?value=100

{
    "": [
        "A non-empty request body is required."
    ]
}

I am sure the problem lies with they way my Postman is setup but it looks correct.

enter image description here

Any suggestions? Thank you!

Updated per suggestion

I added key/value to the body and received "The input was not valid."

enter image description here enter image description here

PKonstant
  • 834
  • 2
  • 15
  • 32

2 Answers2

3

You do not need to use Query string.You need to post the string with raw json, and do not forget the double quotation marks.Refer to here to get more details on post methods by Postman. enter image description here

Ryan
  • 19,118
  • 10
  • 37
  • 53
  • That worked! Thank you. How do you do the same thing but with two parameters in the function like this "Put(int id, [FromBody] string value) { }" ? – PKonstant Nov 27 '18 at 14:12
  • You could see [HttpPut("{id}")] attribute routing above the action, then you just need to select `PUT` and pass the id from url like `https://localhost:44363/api/values/1` – Ryan Nov 28 '18 at 01:38
0

The error message you're receiving is very descriptive of the problem - you haven't provided a body in your request.

Click "Body" in Postman (next to "Headers") then select the type of body you want to send, for example x-www-form-urlencoded and then add a key/value pair beneath, e.g. test and hello world.

Hit "Send".

Matt
  • 720
  • 8
  • 15