Attach XML object to REST POST in asp.net REST starter kit
I'm using the REST starter kit in asp.net for the first time and having a bit of trouble.
I've created some XML...
String newOrganizationStrin = "<somexml></somexml>";
XmlDocument newOrganizationXml = new XmlDocument();
newOrganizationXml.LoadXml(newOrganizationString);
Then I create an httpClient...
HttpClient http = new HttpClient("https://companyname.capsulecrm.com/api/");
http.TransportSettings.Credentials = new NetworkCred开发者_开发百科ential("APIKEY", "PASSWORD");
Now I need to use http.POST() to post the xml to the correct URL. The overloaded method I need I think is (string url, httpContent body). So I guess the missing piece of the puzzle is how to convert the xml to an httpContent, which I can't seem to instantiate.
Any ideas?
Jon
If you are really starting with a string, the easiest way is
var content = HttpContent.Create("<somexml></somexml>","application/xml");
The other way is to use XElement
var content = HttpContentExtensions.Create(XElement.Parse("<somexml></somexml>"));
Sorry, just found the answer -
HttpContent content = HttpContentExtensions.CreateXmlSerializable(newOrganizationXml);
精彩评论