PostAsXmlAsync serialization

I have an API call that responds correctly to an XML message and deserializes the object properly as a parameter. However, when I try to Post programmatically, I still get null as a parameter in my API function.

client.Timeout = System.TimeSpan.FromSeconds(200);
client.BaseAddress = new Uri("http://localhost:59118/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));

...

XmlReader rdr = cmd.ExecuteXmlReader();
Record postValRec = null;
while (rdr.Read())
{
    XmlSerializer serializer = new XmlSerializer(typeof(Record));
    postValRec = (Record)serializer.Deserialize(rdr);
}
rdr.Close();
conn.Close();                

try
{
    response = await client.PostAsXmlAsync("api/Submission", postValRec);
    string html = await response.Content.ReadAsStringAsync();
    Console.WriteLine(html);
}

I checked that postValRecis the correct object, so this is not a problem with this.

When executed client.PostAsXmlAsync, the API function gets the value "null" for the parameter. However, manually moving the object through Postman, since XML works fine, so I'm a bit puzzled.

+4
source share

Source: https://habr.com/ru/post/1544806/


All Articles