I have a simple back-end API written in Asp.Net Core that receives a string:
[HttpPost]
public IResult Post([FromBody] string value)
{
//do something with value...
}
And my front-end javascript looks like this:
const request = {
method: 'POST',
mode : "cors",
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ amount, currency })
};
const response = await fetch(link, request);
const data = await response.json();
It seems all easy peasy, but I get the following errors:
"Unexpected character encountered while parsing value: {. Path '', line 1, position 1."
"The value field is required."
The problem is in the body, that is sent as JSON object:
{ "amount":123, "currency":"eur"}
Apparently the parsing creates problems.
If, with Postman, I send this string inside the body:
"{ \"amount\":123, \"currency\":\"eur\"}"
Everything works!
If I send it via javascript, it doesn't :(
So, my question is: clearly c# is messing up with the type of the data in the body. It is expecting a string but it is receiving a JSON (even though, JSON.stringfy makes a string, but somehow it seems parsed back to JSON somewhere in between).
Can I change the type on C# to something like JSON and everything will automagically work? Is there a way to send a JSON looking string via javascript without this unwanted conversion in between?
Additional info:
@StrikeGently way does not work. I get an error message saying that only 1 parameter can be passed with the [FromBody] attribute.
@joacoleza way might work, but I would like to avoid it as it would fill my code with tens of small classes describing the input of the function.
This works. But my eyes bleed:
body:"'" + JSON.stringify({amount, currency}) + "'"
Is this the most elegant solution to pass a string?
Even though @joacoleza's answer does work, it was not feasible in one of my cases (the object that I am receiving is huge (~200 fields) and I only need to extract 5. Also it might change quite often and I'd have to adjust my object even though I am not really touched by the changes.
I finally found the solution myself:
As already said, .Net tries to parse the content of the body to the requested object:
This requires a string object. If we want to send a JSON and then parse it, we can send it in this way:
'{"field":"value", "field2":"value2"}'But if we do not control the content of the body in the request and we are receiving a X object (or different object that we might want to parse in run-time) we just need to change the type of the function argument to JObject:
The parsed JObject will contain the whole structure of the JSON, but it will still give us enough freedom to do whatever we want with the raw version, without having to cast it to a POCO just for the transition:
var amount = value["amount"];This is what I was looking for.