Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xml POST with RestSharp

Tags:

xml

restsharp

I am using RestSharp to POST the following:

POST API/storage
Content-Type: application/xml
<StorageDocument xmlns="http://xml.somename.com/schema/somename">
  <type>LOCAL</type>
  <method>
    <uri>file:///path/to/files/</uri>
    <read>true</read>
    <write>true</write>
    <browse>true</browse>
    <type>NONE</type>
 </method>
 <autoDetect>true</autoDetect>
</StorageDocument>

My code:

private void button2_Click(object sender, EventArgs e)
    {
        var client = new RestClient();

        client.BaseUrl = new Uri(baseUrlString);
        client.Authenticator = new HttpBasicAuthenticator("admin", "admin");

        var request = new RestRequest("API/storage", Method.POST);

        request.AddParameter ("Content-Type", "application/xml");
        request.RequestFormat = DataFormat.Xml;

        string xml =
            "<StorageDocument xmlns=\"http://xml.somename.com/schema/somename\">" + Environment.NewLine +
            "<type>LOCAL</type>" + Environment.NewLine +
            "<method>" + Environment.NewLine +
              "<uri>file:///path/to/files/</uri>" + Environment.NewLine +
              "<read>true</read>" + Environment.NewLine +
              "<write>true</write>" + Environment.NewLine +
              "<browse>true</browse>" + Environment.NewLine +
              "<type>NONE</type>" + Environment.NewLine +
            "</method>" + Environment.NewLine +
            "<autoDetect>true</autoDetect>" + Environment.NewLine +
            "</StorageDocument>";


        request.AddBody(xml);

        IRestResponse response = client.Execute(request);

        MessageBox.Show(Convert.ToString(response.Content));
    }

When I post I get "HTTP 415 Unsupported Media Type". I have tried many things but cannot figure out where is the issue. I believe it is with xml or body of the request but cannot find the solution.

Any ideas? Thanks in advance

like image 218
Art Avatar asked Dec 06 '22 17:12

Art


2 Answers

It could be useful for someone in my situation, this code worked

private void button2_Click(object sender, EventArgs e)
    {
        var client = new RestClient();

        client.BaseUrl = new Uri(baseUrlString);
        client.Authenticator = new HttpBasicAuthenticator("admin", "admin");


        var request = new RestRequest("API/storage", Method.POST);


        string rawXml = "<StorageDocument xmlns=\"http://xml.somename.com/schema/somename\"><type>LOCAL</type><method><uri>file://home/Testuser/storage/</uri><read>true</read><write>true</write><browse>true</browse><type>NONE</type></method><autoDetect>true</autoDetect></StorageDocument>";

        request.AddParameter("application/xml", rawXml, ParameterType.RequestBody);

        IRestResponse response = client.Execute(request);

        MessageBox.Show(Convert.ToString(response.Content));
    }

The key point is to use AddParameter instead of AddBody, mentioned here RestSharp PUT XML, RestSharp is sending it as GET? by Richard Friend

like image 82
Art Avatar answered Jan 28 '23 19:01

Art


This might help someone... this worked for me

        var postUrl = "https://your-api.com/resource/path";
        string rawXml = "<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>";

        IRestClient client = new RestClient();
        IRestRequest request = new RestRequest
        {
            Resource = postUrl
        };

        request.AddHeader("Content-Type", "text/xml");
        request.AddHeader("Accept", "text/xml");
        request.AddParameter("text/xml", rawXml, ParameterType.RequestBody);

        var response = client.Post<Note>(request);

        Assert.IsNotNull(response.Data);
like image 29
Defria Manda Avatar answered Jan 28 '23 21:01

Defria Manda