Unexpected character encountered while parsing value: . Path '', line 1, position 1
To hit that default endpoint in Postman add the following in the body
"foo"
To use the following
{
"foo": "bar"
}
you would need a class like this
public class MyClass
{
public string Foo { get; set; }
}
then change the Post to
// POST api/values
[HttpPost]
public void Post([FromBody] MyClass value)
{
}
Hope that helps
.net core by default does the binding using naming conventions. Your problem here is that what you are passing as a JSON doesn't match with the value that the action method receives. Change the parameter name to foo
and it should work
// POST api/values
[HttpPost]
public void Post([FromBody] string foo)
{
}
Now with .net core 3.1 the default serializer is not Newtonsoft, now passing a string like this
{
"foo": "bar"
}
will give you a parsing error when you are using a string as the parameter on the FromBody attribute. Developers tend to wrap the content in a class so the binding, in that case, works well fine. Another approach is pass an object as the parameter but you will need to deserialize it inside the action method instead of .net core doing the binding for you.
// POST api/values
[HttpPost]
public void Post([FromBody] object foo)
{
//deserialize the object into your class or if it is a string. call foo.ToStrig() and get the value you need from there
}
Hope this helps