I've created an out of the box .NET Core 2.2 solution and run it. As in:
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?
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.
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 .
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. 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
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)
{
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With