简体   繁体   中英

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.

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.

Having message as list will work -

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

    public string from { get; set; }

    public List<message> message { get; set; }
}

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

    public string body { get; set; }
}

Check the very last answer here -

How to post an array of complex objects with JSON, jQuery to ASP.NET MVC Controller?

If you face issues with list, try this -

Can RestSharp send a List<string> in a POST request?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM