Content from WebRequest.GetResponse... Why can't I see it?

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 ?

//Call the service
  var request = (HttpWebRequest)WebRequest.Create($"{host}{resource}");  
  request.Method = "GET";
  //listen to response
    var response = (HttpWebResponse)request.GetResponse();
        case HttpStatusCode.OK:           
          using (var streamReader = new StreamReader(response.GetResponseStream()))
              var result = streamReader.ReadToEnd();
              dynamic desResult =  JsonConvert.DeserializeObject(result);
              if(desResult.updated == "true")
                wasSuccessful = true;
              this.PublishInfoMessage(" Response: " + desResult.message, Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual, "", "");
              //set variables for UD05 rec update for "success"
  catch(Exception ex)
    this.PublishInfoMessage(codeident + "Bad Response: " + ex.Message + $" {host}{resource}", Ice.Common.BusinessObjectMessageType.Information, Ice.Bpm.InfoMessageDisplayMode.Individual, "", "");
    //set variables for UD05 rec update for "fail"
    wasSuccessful = false;

Read the response with a StreamReader instance :slight_smile:

Also, my god, I wish they’d ship server assemblies with RestSharp or be able to easily use HttpClient.

Hahaha yes to your last point!!!

I was using stream reader and then I still was getting %Response% back when I wrote to console.

I was then thinking it must not be de-serializing it so I just get the variable name.

I will try that, thanks Aaron!

1 Like

Aaron, my response still comes back as %Response% and not as the content I am expecting from bartender.

I don’t understand deeply what the differences are between RestSharp and this class.

I can simply say response.Content with Rest sharp and I see what I am looking for.

When I try to do anything with the HttpClient response I just get %Response%.

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?

Yeah I can try, just more work.

I just find it interesting that RestSharp is so much easier.

I literally type


And I see what I need.

My question is, what is the equivalent of that, but using the HttpClient web response?

Are you using RestSharp or HttpClient?

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

Here it is with restsharp:

            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);

Here it is with a System.Net.WebRequest:

Note that I don’t have a response section because I don’t know how to do it.

			var webRequest = System.Net.WebRequest.Create(string.Format("http://localhost/Integration/EpicorBartenderIntegration/Execute"));
			webRequest.Method = "POST";
			webRequest.ContentType = "application/xml";
			using (var streamWriter = new System.IO.StreamWriter(webRequest.GetRequestStream()))

 /// <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:
                            dynamic content = JsonConvert.DeserializeObject(response.Content);
                            var value = content;
                            return Ok(content);
                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

I need to add this- pretty sure. I’m out for the night though, thank you Aaron for taking the time to post some snippets. Extremely helpful.

I am slowly piecing together all of this. Tech is so crazy and cool.

Aaron, do you have an example of this not using RestSharp?

Aaron, I figured out that the response I am getting from Bartender (which is empty) is the correct response when it fails :cry: 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.

1 Like