Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building XML request with RestSharp

I am attempting to work with a REST API using RestSharp and C#. The documentation for the API that I am using gives a sample XML request:

<?xml version='1.0' encoding='UTF-8'?>  
<messages>  
 <accountreference>EX0000000</accountreference> 
 <from>07700900654</from>
 <message>  
  <to>07700900123</to>  
  <type>SMS</type>  
  <body>Hello Mr Sands.</body>
 </message>  
 <message>  
  <to>07700900124</to>  
  <type>SMS</type>  
  <body>Hello Mr Mayo.</body>
 </message>  
</messages>

I am struggling to understand how to build the request in the format that they want (multiple elements called "message")

I have created these classes for RestSharp to serialize:

public class messages
{
    public string accountreference { get; set; }

    public string from { get; set; }

    public message message { get; set; }
}

public class message
{
    public string to { get; set; }

    public string body { get; set; }
}

And here is my RestSharp code:

var client = new RestClient("http://api.url.com/v1.0")
                         {
                             Authenticator =
                                 new HttpBasicAuthenticator(
                                 UserName,
                                 Password)
                         };

var request = new RestRequest("theresource", Method.POST) { RequestFormat = DataFormat.Xml };

request.AddBody(
    new messages
        {
            accountreference = Configuration.AccountReference,
            from = Configuration.From,
            message =
                new message { to = Configuration.Message.To, body = Configuration.Message.Body }
        });

var response = client.Execute(request);

This works great when I have only 1 message element, but I don't know how to create multiple message elements without having them nested in an array, which doesn't work with the API.

like image 627
Glen Thomas Avatar asked Dec 30 '15 10:12

Glen Thomas


People also ask

What is RestSharp used for?

RestSharp is a comprehensive, open-source HTTP client library that works with all kinds of DotNet technologies. It can be used to build robust applications by making it easy to interface with public APIs and quickly access data without the complexity of dealing with raw HTTP requests.

How does Web API send XML data?

If you want to send XML data to the server, set the Request Header correctly to be read by the sever as XML. xmlhttp. setRequestHeader('Content-Type', 'text/xml'); Use the send() method to send the request, along with any XML data.

How do you add parameters in RestSharp?

var client = new RestClient("http://localhost"); var request = new RestRequest("resource", Method. POST); request. AddParameter("auth_token", "1234"); request. AddBody(json); var response = client.


1 Answers

By default RestSharp is using its own serializer but it also packs the DotNetSerializer so you achieve your goal by changing the serializer like this:

var request = new RestRequest("theresource", Method.POST) 
{ 
    RequestFormat = DataFormat.Xml, 
    XmlSerializer = new RestSharp.Serializers.DotNetXmlSerializer() 
};

Then you can use a list of message objects and decorate it with XmlElement attribute:

public class messages
{
    public string accountreference { get; set; }

    public string from { get; set; }

    [XmlElement("message")]
    public List<message> messageList { get; set; }
}


public class message
{
    public string to { get; set; }

    public string body { get; set; }
}

Then you can change the last bit to add multiple messages:

request.AddBody(
    new messages
    {
        accountreference = "ref",
        from = "from",
        messageList = new List<message>() {
                new message { to = "to1", body = "body1" },
                new message { to = "to2", body = "body2" }
                }
    }); 

which would produce (I got the XML by checking request.Parameters[0].Value):

<?xml version="1.0" encoding="utf-8"?>
<messages>
  <accountreference>ref</accountreference>
  <from>from</from>
  <message>
    <to>to1</to>
    <body>body1</body>
  </message>
  <message>
    <to>to2</to>
    <body>body2</body>
  </message>
</messages>

I guess this is the XML format you've been looking for.

like image 188
Volkan Paksoy Avatar answered Sep 23 '22 14:09

Volkan Paksoy