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
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
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With