i have an application in .NET 5, I'm trying to call an API with HttpClient using method PostAsJsonAsync, like this:
var jsonSerializerOptions = new JsonSerializerOptions
{
IgnoreNullValues = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true,
};
var result = await client.PostAsJsonAsync("subjects", userRegistrationRequest, jsonSerializerOptions);
but I obtain always status code 500, so I tried to inspect the body request with fiddler and I noticed strange chars at the start and at the end of the body, like this:
5F
{
"id": "82240631-8c12-4b77-8626-7d36f93ceacc",
"language": "en",
"type": "subject"
}
0
But if I use classic PostAsync like this:
var result = await client.PostAsync("subjects", new StringContent(JsonSerializer.Serialize(userRegistrationRequest, jsonSerializerOptions),
Encoding.UTF8, MediaTypeNames.Application.Json));
the body is correct:
{
"id": "82837dba-c86d-4cf2-9c98-7f402b2761a1",
"language": "en",
"type": "subject"
}
and it works fine. Why does this happen? Thank you.