'Unexpected character encountered while parsing value: . Path '', line 1, position 1

I've created an out of the box .NET Core 2.2 solution and run it. As in:

  1. Create Project, selecting ASP.NET Core Web Application.
  2. Select API as the project template.
  3. F5

This gives me this POST handling code in the default ValuesController class:

    // POST api/values
    [HttpPost]
    public void Post([FromBody] string value)
    {
    }

Through Postman, I POST

{
"foo": "bar"
}

and receive

Unexpected character encountered while parsing value: . Path '', line 1, position 1.

The error occurs before even reaching "my" (out of the box) code. I see a number of references to serialization, but this is occurring before I ever get a chance to touch the payload.

What (probably dreadfully simple) configuration is missing?



Solution 1:[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

Solution 2:[2]

.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. Another approach is to 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

Solution 3:[3]

You can send the JSON without the {} brackets and key in the Body

From:

{
  "foo": "bar"
}

To simply sending:

"bar"

This will now work with a string parameter in your Controller Endpoint (just like before):

// POST api/values
[HttpPost]
public void Post([FromBody] string value)
{
}

More info

When you're sending JSON in brackets you're sending an object with keys and values that will be received by the Controller Endpoint. Sending Json this way requires a class on the receiving end with matching properties to the JSON object in order to read the data correctly.

This solution sends only the actual value as a string and is why you don't need a class to receive it.

credit: https://stackoverflow.com/a/57919764/15337673

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SWilko
Solution 2
Solution 3