I have a strange issue using IhttpClientFactory named client.
The "POST" body parameter are changing it's case to camel "initCap". I have written some Handlers (delegate handler) for adding "http headers", "exception handling" and "audit logging".
The parameters are changing the case to camelcase.
My Model class
public class PostRequest
{
[DataMember(Name = "Request", EmitDefaultValue = false)]
public Request? Request { get; set; }
}
I know that the attribute is for Newtonsof json but tried JsonProperty as well but not working. .....
to get the response of the post call (inside a generic calss method
var httpContent = await client.PostAsJsonAsync(resource, requestBody, cancellationToken);
return await httpContent.Content.ReadFromJsonAsync<T>(cancellationToken: cancellationToken);
Anyone faced similar issue?
PostAsJsonAsync
uses JsonSerializerDefaults.Web
for serializer options by default which will result in Camel case property names and data contracts (DataMemeberAttribute
) are not supported by System.Text.Json
. If you want use another set of settings for serialization (i.e. Pascal case default serialization) - you can provide them. For example:
var httpContent = await client.PostAsJsonAsync(
resource,
requestBody,
new JsonSerializerOptions(), // TODO: configure as needed and reuse the options
cancellationToken);
Or use the JsonPropertyNameAttribute
(will need to mark all properties which require "non-default" handling). For example:
public class PostRequest
{
[JsonPropertyName("Request")] // should be Pascal case no matter which System.Text.Json settings used
public Request? Request { get; set; }
}
See also: