For HttpPost function
public async Task<Result> PostAsync([FromBody]Request request, [FromHeader(Name = "Cs-Auth")] string authKey=null)
{
try{
HttpResponseMessage response = await httpClient.PostAsJsonAsync(_config.ApiUrl, request);
response.EnsureSuccessStatusCode();
string responseText = await response.Content.ReadAsStringAsync();
Result result = JsonSerializer.Deserialize<Result>(responseText);
return ProcessResult(result);
}
catch(Exception e)
{
_logger.LogError(e.ToString());
throw;
}
}
The Request class:
public class Request
{
[JsonPropertyName("text")]
public string Text { get; set; }
[JsonPropertyName("user_id")]
public string user_id { get; set; }
[JsonPropertyName("rule_id")]
public string PolicyId { get; set; }
public Policy Policy {get; set;}
}
The Json body:
{
"text": "sample",
"user_id": "3455643",
...
}
But the request I got in PostAsync looks like
I was expecting "text", not "Text".
I donot want to change property name "Text" in Request Class to "text", what do I need to do to define serialization/deserialization behavior?
CodePudding user response:
Everything is working properly, your debugger just show data BEFORE changing. try this in your debugger
var jsonRequest= JsonConvert.SerializeObject(request);
var content = new StringContent(jsonRequest, Encoding.UTF8, "application/json");
var response = await client.PostAsync(_config.ApiUrl, content);
jsonRequest
{
"text": "sample",
"user_id": "3455643",
...
}