Reputation: 673
I'm having an issue trying to make a POST using Rest Sharp. I am trying to make a call to a web service that doesn't exactly conform to modern standards and I need to be able to post the request XML in the body of my post.
If I simply do request.AddBody(xmlObject) on the RestRequest object, a parameter called text/xml is added to the request. In this case, I get an error response from the server saying "Exception thrown: Content is not allowed in prolog". This tells me that the server is trying to process the key of the parameter (text/xml=) along with the value.
When I use the REST Console in chrome with the desired xml request in the RAW body field, everything works as expected.
Any ideas on how I could make this work with Rest Sharp?
Edit: Looks like I need to use request.AddFile("name", bytes_to_add, "file_name"). However, when I try to do that, I get an exception ("System.InvalidOperationException: This property cannot be set after writing has started."), which looks like an issue with RestSharp that was never resolved. I might just have to go with making requests with the HttpWebRequest library.
Upvotes: 0
Views: 3750
Reputation: 3302
You might be overthinking it. RestSharp.AddBody takes an object as a parameter, and automatically serializes it to the correct format using the JSON or XML serializers, depending on the RequestFormat property.
It doesn't make sense to add the XML directly. Part of the magic of REST is that it supports multiple formats, not just XML. RestSharp is written with that in mind, so all of its public interfaces just use POCOs.
The simple solution is just not to serialize the object. You don't want to add xmlObject, just pass in your real C# object to the AddBody method and let it handle the serializing.
Upvotes: 1