Solid.Http.Extensions.Xml Usage - SOLIDSoftworks/Solid.Http GitHub Wiki

Adding xml content to the request

Adding xml serialized content to the request can be done as follows

var client = context.RequestServices.GetRequiredService<ISolidHttpClientFactory>().CreateWithBaseAddress("http://SomeBaseUri");
var obj = new ObjectToSerialize();
var response = await client
    .PostAsync("some/path/that/accepts/xml/post", cancellationToken)
    .WithXmlContent(obj)

Deserializing xml response content

Getting serialized xml to objects can be done as follows

public class ValuesController : Controller
{
    private SolidHttpClient _client;
    public ValuesController(ISolidHttpClientFactory factory)
    {
        _client = factory.CreateWithBaseAddress("http://mockbin.com");
    }
    [HttpGet]
    public async Task GetAsync()
    {
        var resp = await client
            .GetAsync("request")
            .WithHeader("Accept", "application/xml")
            .As<MockbinResponse>();
        return Ok(resp);
    }
}

Where the models could look something like this

    [DataContract(Name = "headers", Namespace = "")]
    public class MockbinHeaders
    {
        [DataMember(Name = "host")]
        public string Host { get; set; }
    }

    [DataContract(Name = "response", Namespace = "")]
    public class MockbinResponse
    {
        [DataMember(Name = "startedDateTime", Order = 1)]
        private DateTime Started { get; set; }
        [DataMember(Name = "clientIPAddress", Order = 2)]
        public string ClientIPAddress { get; set; }
        [DataMember(Name = "method", Order = 3)]
        public string Method { get; set; }
        [DataMember(Name = "url", Order = 4)]
        public string Url { get; set; }
        [DataMember(Name = "headers", Order = 5)]
        public MockbinHeaders Headers { get; set; }
    }