Does anyone have any C# documentation of working with XML/JSON and calling a 3rd party API?

In this example I am indeed passing the parameters as query parameters. If your API consumes from the body of the call, you could wrap up the parameters and add them to the body. I have an example (not Epicor but using C#) below adding parameters to the body of an API call using the RestSharp library.

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

In my first example, I am using a HttpWebRequest because Epicor doesn’t ship with RestSharp available out of the box in the server assemblies (:expressionless:), so I’m forced to use ancient tech to accomplish that. However, it is possible, just don’t have a great example:

C# JSON Post using HttpWebRequest - Stack Overflow

For your parameters, I don’t believe in doing hard-coded serialization like some people do but an easy way to accomplish that is to add your parameters to a Dictionary and serialize the whole thing into JSON :

//add custom field values to this dictionary
var customFields = new Dictionary<string,string>;
{
	{"Physician","Dr. Test"}
	,{"Patient", "Test Patient"}
	,{"Graft Requested", "Femoral Core, 10mm"}
	,{"Graft Offered", "Femoral Core, 10mm"}
	,{"Graft ID", "123456-999"}
	,{"Release Date", "10/18/2018"}
	,{"Expire Date", "10/30/2018"}
	,{"Patient Size Msg", "TW=N/A, W=N/A, L=N/A (bunch of text here)"}
	,{"Donor Size Msg", "TW=N/A, W=N/A, L=N/A (bunch of text here)"}
	,{"QuoteDtl Comment", "Please see dissection sheet for specific sizing"}
	,{"ASC", "Aaron Moreng"}
	,{"Offer Date", "10/18/2018"}
};
								

Or if you like even less code, you can just use an anonymous type to build your body and serialize it:

 var taskHead = new
  {
      planId = bucket.PlanID,
      bucketId = bucket.BucketID,
      title = "New Task from Teams Bridge"
  };
  var content = JsonConvert.SerializeObject(taskHead);

Because you won’t have the ability to create a model for your response, I’d also recommend using the dynamic type when reading your response, especially if you are accessing nested data within it.

RE: Nested json; you’ll be able to do this with ease using anonymous types. That’s definitely what I’d recommend for this.
Anonymous Types | Microsoft Docs

4 Likes