Reputation: 846
I recently implemented RestSharp instead of using plain old HTTP client in .NET Core. This was too help with some OAuth1 authentication problems and it worked great.
However, one requirement I need to support is now broken - a call to an old version of NAV that still uses a SOAP envelope and XML.
My RestSharp request is built like this (partially redacted):
var request = new RestRequest(transmission.Url.PathAndQuery, Helper.GetRestSharpMethod(transmission.Method), dataType);
request.AddParameter("text/xml", body, "text/xml", ParameterType.RequestBody);
Where dataType is Datatype.Xml and body is my soap envelope as XML text. I've tried "application/xml". I've also tried using request.AddBody and request.AddBodyXml - but they just caused more issues.
The endpoint (which we know works with the old request) returns an issue with being unable to read data from inside the SOAP envelope (which again we know is there, because it's in the parameters of the request).
Can anybody suggest any ways of fixing this issue and still using rest sharp? Or is this not really intended to be supported, and I'd be better off using plain HTTP Client for this specific part?
Thanks.
EDIT:
Concerning the old code, SOAP envelope text is identical. The difference is that instead of using RestSharp request.AddBody(), I built standard HttpRequestMessage, then set the HttpContent as new StringContent(content)
This was sent using standard .NET Core httpclient
var response = await client.SendAsync(request);
Upvotes: 1
Views: 8757
Reputation: 232
I had similar issues with differing results based on .NET 4.8 vs .NET 7.0... and different versions of RestSharp. Here are the two variants with success based on the differing versions.
var client = new RestClient(targetUri);
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "text/xml");
request.RequestFormat = DataFormat.None;
request.AddParameter("SOAPAction", postData, ParameterType.RequestBody);
var response = client.Execute(request);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
return response.Content;
}
else
{
throw new Exception("Soap Request Failed");
}
var client = new RestClient(targetUri);
var request = new RestRequest();
request.AddHeader("Content-Type", "text/xml");
request.RequestFormat = DataFormat.None;
request.AddStringBody(postData, DataFormat.Xml);
var response = await client.ExecutePostAsync(request);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
return response.Content;
}
else
{
throw new Exception("Soap Request Failed");
}
Upvotes: 0
Reputation: 846
Ok finally got the to the bottom of this - posting here just in case anybody else has similar issues.
First of all - yes, it is supported.
However, there are a number of gotchas along the way - RestSharp
automagically handles escaped URL
strings. Don't escape them
yourself.
Use request.AddParameter
with ParameterType.RequestBody
instead of
request.AddBody.
When creating the rest client, be sure to give it the full URI
when
using basic authentication (I was using partial base address for
OAuth1
, and carried it over).
When creating IRestRequest
, be careful with supplying additional parameters like DataFormat
, as removing this seemed to help resolve the problem.
Hope this helps.
Upvotes: 3