I have an .NET 6 API endpoint that needs to be able to take any valid json, and then log it.
The endpoint is post, and I need to be able to send the data in the body.
My idea for doing this, was the following:
public async Task<IActionResult> LogSomeJSONendpoint([FromBody] JObject payload)
{
Log.information(payload));
}
The logging converts the JObject to a string and then logs it.
but, I call this endpoint, give it the following json:
{"name":"John", "age":30, "car":null}
Then I get this response:
{
"title": "One or more validation errors occurred.",
"status": 400,
"errors": {
"$.name": [
"The JSON value could not be converted to Newtonsoft.Json.Linq.JToken. Path: $.name | LineNumber: 0 | BytePositionInLine: 14."
]
}
}
hmmm, what? I'm fairly sure what I posted was valid JSON, so I can't figure out why it can't take it.
If JObject does'nt work for this task, how else could I solve it? I just need to take any valid JSON, turn it into a string, and then log it