REST call to Epicor Function throws System.IO.IOException

I’m having a problem calling an Epicor function within C#. Below is thte code I’m using in the call:

private static async void CallPostFunction()
{
try
{
string ApiKey = Properties.Settings.Default.APIKey;
var RestClient = new RestClientBuilder()
.SetAppServerUrl(Properties.Settings.Default.AppServerURL)
.SetCompanyId(Properties.Settings.Default.CompanyID)
.SetDefaultApiKey(ApiKey)
.UseBasicAuthentication(Properties.Settings.Default.BatchEntryPerson, Properties.Settings.Default.BatchEntryPass)
.Build();

            var FunctionParameters = new RestContent(new { GroupID = BatchGroupID });

            var FunctionResponse = await RestClient.Function.PostAsync(Properties.Settings.Default.RESTLibrary, Properties.Settings.Default.RESTFunction, FunctionParameters, true);
            if (FunctionResponse.IsSuccessStatusCode != true)
            {
                Console.WriteLine(FunctionResponse.StatusCode.ToString() + " " + FunctionResponse.Content);
            }
        }
        catch (Exception e)
        {
            throw new Exception("REST Call", e);
        }
</language>

When the call is executed, I get a System.IO.IOException from System.Private.CoreLib.dll, then a System.Exception from the project. I can execute the same call from Swagger with no error, I just don’t know what I’m doing wrong in the code. Would anyone have an idea?

Maybe try catching a System.IO.IOException rather than using base Exception to get more information about what exactly System.IO is having trouble with. IOException will at least tell you what part of the io is having issues and might help you debug a little more efficiently.

If the exception is happening during the post call then it couldn’t hurt to double check everything in your settings file is correct. Just a guess. Catching the IOException should point you in a better direction.

try
{
     string ApiKey = Properties.Settings.Default.APIKey;
     var RestClient = new RestClientBuilder()
                                       .SetAppServerUrl(Properties.Settings.Default.AppServerURL)
                                       .SetCompanyId(Properties.Settings.Default.CompanyID)
                                       .SetDefaultApiKey(ApiKey)
                                       .UseBasicAuthentication(Properties.Settings.Default.BatchEntryPerson, Properties.Settings.Default.BatchEntryPass)
                                       .Build();

     var FunctionParameters = new RestContent(new { GroupID = BatchGroupID });

     var FunctionResponse = await 
     RestClient.Function.PostAsync(Properties.Settings.Default.RESTLibrary, 
     Properties.Settings.Default.RESTFunction, FunctionParameters, true);
     if (FunctionResponse.IsSuccessStatusCode != true)
     {
          Console.WriteLine(FunctionResponse.StatusCode.ToString() + " " + FunctionResponse.Content);
     }
}
catch (System.IO.IOException e)
{
     // If just catching and then immediately rethrowing then consider not catching the 
     // exception here to avoid making it look like exceptions are happening in multiple areas.
     // Maybe catch where you are calling CallPostFunction instead.  Doing it this way will just
     // generate a new exception with an identical exception in the InnerException property.  
     // Just a suggestion.
     throw new System.IO.IOException("REST Call", e);
}
catch (Exception e)
{
     // Keeping this catch will cover any other unhandled exceptions that might come up
     throw new Exception("REST Call", e);
}

I think we got this sorted in another thread.

It’s a broken version of the resthelper from epicor.