Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 233
John Spiegel Avatar asked Aug 27 '19 16:08

John Spiegel


People also ask

How do you deserialize an object in C#?

NET objects (deserialize) A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer. Deserialize method.

What is Jsonconvert DeserializeObject?

DeserializeObject<T>(String,JsonConverter[]) Deserializes the JSON to the specified . NET type using a collection of JsonConverter. DeserializeObject(String, JsonSerializerSettings) Deserializes the JSON to a .


3 Answers

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

like image 132
SWilko Avatar answered Oct 28 '22 08:10

SWilko


.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

like image 27
Zinov Avatar answered Oct 28 '22 06:10

Zinov


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

like image 5
tiny_mike Avatar answered Oct 28 '22 08:10

tiny_mike