0

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.

Jacob Huggart
  • 663
  • 2
  • 11
  • 30
  • Can you post what the serialized request looks like? The easiest way is to capture it with Fiddler. That exception usually means there's some data before the opening tag in the request. – Tim Copenhaver Nov 21 '12 at 15:10
  • I got it working by using an HttpWebRequest and writing directly to the request stream. I'm still using the Rest Sharp serializer, so I know that the object is serialized correctly. But I'm not a fan of using HttpWebRequest directly. So I've cloned the Rest Sharp code to see if I can figure out how to fix the exception that is being thrown. – Jacob Huggart Nov 21 '12 at 15:14
  • I think this is a duplicate of http://stackoverflow.com/questions/5095692/how-to-add-text-to-request-body-in-restsharp – Bent Tranberg Jan 17 '15 at 07:04

1 Answers1

1

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.

Tim Copenhaver
  • 3,282
  • 13
  • 18
  • I should have been clearer in naming the object passed to AddBody. I'm not serializing the object myself, I should have just called it requestModel or something like that. The Rest Sharp serialization works beautifully, but the server is responding with the error mentioned in my post. – Jacob Huggart Nov 21 '12 at 14:11
  • It makes perfect sense to post xml directly when I want to debug when something fails and BizTalk is the client. – Thomas Eyde Mar 06 '15 at 13:43
  • @ThomasEyde I don't believe posting XML directly is the right approach to solve your problem. You may use a custom serializer to see or control what is happening, or view it with something like Fiddler or Wireshark. It's a mixture of concerns (and bad OO design) to worry about XML serialization in the same application code that's trying to send requests. – Tim Copenhaver Mar 07 '15 at 15:48
  • @TimCopenhaver, I think you misunderstand my needs: I have a customer which calls my api from BizTalk. Something fails and they can only provide me the xml. Now I have to test / debug with that piece of xml, and I want to do it in code. Mostly because I think that's easier than setting up fiddler, but also because code can be saved between reboots. This is about debugging, not writing an api client. – Thomas Eyde Mar 07 '15 at 16:31
  • Ah, I understand your situation then. Really, for debugging, code is not always your best option. Other tools such as SoapUI are already designed to handle exactly this case, so I could see why they wouldn't include it in RestSharp. – Tim Copenhaver Mar 09 '15 at 15:38