1

I am a newbie in using Refit. I am using Refit now to call a REST Api that expects xml as input. This works but it seems that Refit automatically adds the well known xml preamble to describe encoding.

I want to send the xml element without preamble (as required by the target system). How do I do this? Here is my code in the startup class:

    var settings = new RefitSettings
    {
        ContentSerializer = new XmlContentSerializer()
    };
    services.AddRefitClient<IItemApi>(settings)
        .ConfigureHttpClient(c => c.BaseAddress = new Uri("http://127.0.0.1:5000"));

Here is my data class and just used interface

public class PayLoad
{
    public string A { get; set; }
    public string B { get; set; }
}

public interface IItemApi
{
    [Post("/target/{id}")]
    Task<ApiResponse<string>> PostItemAsync(string id, [Body] PayLoad item,
        CancellationToken cancellationToken = default);
}

Here is an example of a post call:

        var result = itemApi.PostItemAsync("X",new PayLoad
        {
            A = "A",
            B = "B"
        });

This is the raw request:

POST http://127.0.0.1:5000/target/X HTTP/1.1
Content-Type: application/xml; charset=utf-8
Content-Length: 76
Host: 127.0.0.1:5000

<?xml version="1.0" encoding="utf-8"?><PayLoad><A>A</A><B>B</B></PayLoad>

How can I change my code in such a way this part

<?xml version="1.0" encoding="utf-8"?>

is not in the request anymore?

Daan
  • 2,478
  • 3
  • 36
  • 76
  • I'd hope you can't, as without that part it is not valid xml. By the way, that part is also called [XML-Declaration](https://xmlwriter.net/xml_guide/xml_declaration.shtml). EDIT: I just read that only in version="1.1" it's mandatory, but even in version="1.0" it is heavily recommended – MindSwipe Mar 27 '19 at 14:47
  • This is not how my target system expects it. Moreover, it is also not very usual for web requests. For example the PostXmlAsync of HttpClient does not do it. In addition, it is redundant. The encoding is already mentioned as Http header info (in the Content-Type). – Daan Mar 27 '19 at 15:01
  • The encoding actually isn't necessary, even in v1.1, so you can omit that. And what kind of XML deserializer does your target system use if it isn't expecting an xml declaration? – MindSwipe Mar 27 '19 at 15:03
  • I know only one thing of the target system (created by an external party): a request works if I just send it without the preamble. – Daan Mar 27 '19 at 15:21
  • Ok, then what I would do now is 1. Get in contact with said external party and ask them what's up with them not accepting valid XML and then 2. Serialize the XML to a string, remove the declaration and then send it – MindSwipe Mar 27 '19 at 15:23

1 Answers1

0

You need to configure your xml serializer to skip it, so you'd change the settings in your example to be like this:

var settings = new RefitSettings
    {
        ContentSerializer = new XmlContentSerializer(
            new XmlContentSerializerSettings
            {
                XmlReaderWriterSettings = new XmlReaderWriterSettings
                {
                    WriterSettings = new XmlWriterSettings
                    {
                        OmitXmlDeclaration = true
                    }
                }
            })
    };
Martin
  • 163
  • 1
  • 9