When I use RESTSharp I can write out response.Content from a web request and it will list the response from my web call. For bartender it is a large xml string with a bunch of properties.
When I try to make a web call using System.Net.WebRequest I don’t know what to do to get the same output when I try and write the response.
It just returns %Response% which is strange.
In short, does anyone have any code examples of how to write out a response from a System.Net.WebRequest ?
It’s very possible that’s what the API is returning…there are not really any differences worth mentioning between http clients you use. I wonder if you can dig into the service itself more. Can you call the service from a different client, say Postman or a console app that you can debug to allow you to dig into the response more?
Both, I built a console app with restsharp that sends a post request to bartender web service and I built a console app with http client that sends a post request to the same bartender web service.
Ok, well to read the response it will be in that Content property for sure. Do you mind sharing a little code and screenshots to help spur the discussion? I don’t really know what the issue might be without looking closer
var client = new RestClient("http://localhost/Integration/EpicorBartenderIntegration/");
var request = new RestRequest("/Execute", Method.POST);
request.AddParameter("application/xml", sw, ParameterType.RequestBody);
var response = client.Execute(request);
Console.WriteLine(Convert.ToString(response.Content));
/// <summary>
/// Invokes an Epicor Function from the specific function library
/// </summary>
/// <param name="library">The Library ID associated with the function</param>
/// <param name="functionID">The Function ID to be invoked</param>
/// <param name="fxRequest">The JSON from the call body representing the optional input parameters</param>
/// <returns></returns>
public async Task<IActionResult> InvokeFunction(string library, string functionID, dynamic fxRequest)
{
if (_epiUtils.ValidSession(_epiUtils.sessionID, _licenseType, _path, _user, _apiKey, out string msg))
{
var restClient = new RestClient(_fxPath)
{
RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true
};
var request = new RestRequest($"{library}/{functionID}", Method.POST);
//add any optional request parameters
request.AddParameter("application/json", fxRequest, ParameterType.RequestBody);
//Web Service License
var headerLicense = new
{
ClaimedLicense = _licenseType,
SessionID = _epiUtils.sessionID
};
var header = JsonConvert.SerializeObject(headerLicense);
request.AddHeader("License", header);
request.AddHeader("Authorization", $"Basic {EpiUtils.Base64Encode(_user)}");
request.AddHeader("x-api-key", _apiKey);
IRestResponse response = await restClient.ExecuteAsync(request);
switch (response.StatusCode)
{
case System.Net.HttpStatusCode.BadRequest:
{
dynamic content = JsonConvert.DeserializeObject(response.Content);
var value = content;
return BadRequest(content);
}
case System.Net.HttpStatusCode.OK:
default:
{
dynamic content = JsonConvert.DeserializeObject(response.Content);
var value = content;
return Ok(content);
}
}
}
else
{
return Unauthorized(msg);
}
}
Do we know what type the API is returning? I’m guessing XML so you’ll need to deserialize the XML
I have some work to do man based on looking at your code.
I need to better understand what I am doing.
I tried to change the response type in bartender to JSON and use your de-serialize example from above (not the most recent one you posted) and it gave me an error because the result I was trying to deserialize had a value of %Response% which is not a Json object.
Yeah right man! I’m just a learner, but thanks. Is there any documentation on the Bartender API about responses? I’d try using an XML serializer because it’s clearly not json coming back
Aaron, I figured out that the response I am getting from Bartender (which is empty) is the correct response when it fails and that only Bartender 2019 has this issue.
I thought I was doing something wrong with my web calls or response call that was causing it to be empty.
In short, the API was returning a string that should be xml, but because the document was failing to print, it returned “%Response%” instead… They said it should be more detailed in 2021?
They said you can use %LastErrorMessage% in the Integration>Response page to get errors if there are any for 2019.
Thanks again for all of your help, it had nothing to do with the way I was coding it like I thought it did.