EpicorRESTApiCore - Error when running POST

Jose, got farther this time, but now getting this error:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException
HResult=0x80131500
Message=Cannot perform runtime binding on a null reference
Source=System.Linq.Expressions
StackTrace:
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
at EpicorRestAPI.EpicorRestSession…ctor()
at APOCInvoiceLoad.Program.CallPostFunctionNew() in M:\IT_Volume\SourceCode\CSharp_102700\APOCInvoiceLoad\APOCInvoiceLoad\Program.cs:line 1562
at APOCInvoiceLoad.Program.CreateAPInvoiceBatch(String originalFile) in M:\IT_Volume\SourceCode\CSharp_102700\APOCInvoiceLoad\APOCInvoiceLoad\Program.cs:line 1200

This exception was originally thrown at this call stack:
[External Code]
APOCInvoiceLoad.Program.CallPostFunctionNew() in Program.cs
APOCInvoiceLoad.Program.CreateAPInvoiceBatch(string) in Program.cs

Error is occurring at the point of creating a new EpicorRestSession. Current code is below:

        private static void CallPostFunctionNew()
        {
            EpicorRest.AppPoolHost = "usaz1app008p.am.dir.grpleg.com";
            if (_ServerID == "")
                EpicorRest.AppPoolInstance = "EpicorERP";
            else
                EpicorRest.AppPoolInstance = _ServerID;
            EpicorRest.UserName = Properties.Settings.Default.BatchEntryPerson;
            EpicorRest.Password = Properties.Settings.Default.BatchEntryPass;
            EpicorRest.APIKey = EnvAPIKey;
            EpicorRest.Company = Properties.Settings.Default.CompanyID;
            EpicorRest.APIVersion = EpicorRestVersion.V2;
            EpicorRest.License = EpicorLicenseType.Default;
            EpicorRest.IgnoreCertErrors = true;

            using (EpicorRestSession ses = new EpicorRestSession())
            {
                var smsSend = new
                {
                    GroupID = $@"{BatchGroupID}"
                };

                var rsp = EpicorRest.EfxPost(Properties.Settings.Default.RESTLibrary, Properties.Settings.Default.RESTFunction, smsSend);

                if (rsp.IsErrorResponse)
                    Console.WriteLine(rsp.ResponseError.ToString());
            }
        }

It worked just fine for me, Only thing I can think of is some of your settings are wrong… BUt that’s harad to troubleshoot without having your settings.

here’s a github project

that has a WORKING example of this API all you need to d is adjust the settings. I just tested it and worked like a dream.

Try that?

1 Like

Sure will, thanks!

Downloaded the project as a Zip file, unzipped it and opened the project. When I opened it, it apparently didn’t recognize it had a reference to the EpicorRestHelper DLL. I did look at the settings in the program.cs file, and mine only lacked the Timeout and MaxRequestLogLength properties. I’ll try the example and my project tomorrow, may attach my settings file if I’m still having issues.

1 Like

Oh, also refreshed the Nuget packages and fixed the DLL issues.

Jose, tried my REST call after making adjustments, however I’m getting the below error:

I’m attaching my settings file:

<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="APOCInvoiceLoad.Properties" GeneratedClassName="Settings">
  <Profiles />
  <Settings>
    <Setting Name="CompanyID" Type="System.String" Scope="Application">
      <Value Profile="(Default)">KEN</Value>
    </Setting>
    <Setting Name="OutputDMTPath" Type="System.String" Scope="Application">
      <Value Profile="(Default)"><servername>\EpicorData\DMT\APOCInvoicePost\</Value>
    </Setting>
    <Setting Name="DBConnectionLive" Type="System.String" Scope="Application">
      <Value Profile="(Default)">Data Source=<servername>;Initial Catalog=EpicorERP;Persist Security Info=True;User ID='ErpAdmin';Password='<password>'</Value>
    </Setting>
    <Setting Name="DBConnectionTest" Type="System.String" Scope="Application">
      <Value Profile="(Default)">Data Source=<servername>;Initial Catalog=EpicorERPTest;Persist Security Info=True;User ID='ErpAdmin';Password='<password>'</Value>
    </Setting>
    <Setting Name="DBConnectionPilot" Type="System.String" Scope="Application">
      <Value Profile="(Default)">Data Source=<servername>;Initial Catalog=EpicorERPPilot;Persist Security Info=True;User ID='ErpAdmin';Password='<password>'</Value>
    </Setting>
    <Setting Name="ViewConnectionLive" Type="System.String" Scope="Application">
      <Value Profile="(Default)">Data Source=<servername>;Initial Catalog=EpicorERP_CustomReporting;Persist Security Info=True;User ID='ErpAdmin';Password='<password>'</Value>
    </Setting>
    <Setting Name="ViewConnectionTest" Type="System.String" Scope="Application">
      <Value Profile="(Default)">Data Source=<servername>;Initial Catalog=EpicorERPTest_CustomReporting;Persist Security Info=True;User ID='ErpAdmin';Password='<password>'</Value>
    </Setting>
    <Setting Name="ViewConnectionPilot" Type="System.String" Scope="Application">
      <Value Profile="(Default)">Data Source=<servername>;Initial Catalog=EpicorERPPilot_CustomReporting;Persist Security Info=True;User ID='ErpAdmin';Password='<password>'</Value>
    </Setting>
    <Setting Name="TestServerID" Type="System.String" Scope="Application">
      <Value Profile="(Default)">EpicorERPTest</Value>
    </Setting>
    <Setting Name="PilotServerID" Type="System.String" Scope="Application">
      <Value Profile="(Default)">EpicorERPPilot</Value>
    </Setting>
    <Setting Name="CompanyName" Type="System.String" Scope="Application">
      <Value Profile="(Default)">Company</Value>
    </Setting>
    <Setting Name="BatchFileProd" Type="System.String" Scope="Application">
      <Value Profile="(Default)">\\<servername>\EpicorData\DMT\APOCInvoicePost\DMTAPOCInvoiceProd.bat</Value>
    </Setting>
    <Setting Name="ArchiveDMTPath" Type="System.String" Scope="Application">
      <Value Profile="(Default)">\\<servername>\EpicorData\DMT\APOCInvoicePost\Archive</Value>
    </Setting>
    <Setting Name="CurrencyCode" Type="System.String" Scope="Application">
      <Value Profile="(Default)">BASE</Value>
    </Setting>
    <Setting Name="LineTypeMisc" Type="System.String" Scope="Application">
      <Value Profile="(Default)">M</Value>
    </Setting>
    <Setting Name="RateGroupCode" Type="System.String" Scope="Application">
      <Value Profile="(Default)">CONV</Value>
    </Setting>
    <Setting Name="BatchFilePilot" Type="System.String" Scope="Application">
      <Value Profile="(Default)">\\<servername>\EpicorData\DMT\APOCInvoicePost\DMTAPOCInvoicePilot.bat</Value>
    </Setting>
    <Setting Name="BatchFileTest" Type="System.String" Scope="Application">
      <Value Profile="(Default)">\\<servername>\EpicorData\DMT\APOCInvoicePost\DMTAPOCInvoiceTest.bat</Value>
    </Setting>
    <Setting Name="InputPath" Type="System.String" Scope="Application">
      <Value Profile="(Default)">\\<servername>\EpicorData\APOC\Test\</Value>
    </Setting>
    <Setting Name="BatchEntryPerson" Type="System.String" Scope="Application">
      <Value Profile="(Default)"><userID></Value>
    </Setting>
    <Setting Name="LineTypeReceipt" Type="System.String" Scope="Application">
      <Value Profile="(Default)">R</Value>
    </Setting>
    <Setting Name="AzureContainer" Type="System.String" Scope="Application">
      <Value Profile="(Default)">apocinfosys</Value>
    </Setting>
    <Setting Name="APIKey" Type="System.String" Scope="Application">
      <Value Profile="(Default)"><APIKey></Value>
    </Setting>
    <Setting Name="BatchEntryPass" Type="System.String" Scope="Application">
      <Value Profile="(Default)"><password></Value>
    </Setting>
    <Setting Name="AppServerURL" Type="System.String" Scope="Application">
      <Value Profile="(Default)">https://<servername>/EpicorERP/api/v2/efx/KEN/PostAPInvoice/PostAPGroup/</Value>
    </Setting>
    <Setting Name="RESTLibrary" Type="System.String" Scope="Application">
      <Value Profile="(Default)">PostAPInvoice</Value>
    </Setting>
    <Setting Name="RESTFunction" Type="System.String" Scope="Application">
      <Value Profile="(Default)">PostAPGroup</Value>
    </Setting>
    <Setting Name="NonPOFileHeader" Type="System.String" Scope="Application">
      <Value Profile="(Default)">IdentifierCaseIdCompanyCodeFiscalYearAPOCDocumentTypeERPDocumentTypeDocumentDatePostingDateDocumentNoDocumentHeaderTextCurrencyExchangeRateTaxReportingDateBaselineDatePaymentTermCodePaymentMethodInvoiceReferenceNumberInvoiceReferenceFiscalYearSCBIndicatorVendorNumberISRNumberISRReferencePaymentReferenceAccountTypeInvoiceLineNumberVATTaxCodeTotalAmountLineAmountVATAmountVATRateVendorAssignmentLineAssignmentVendorHeaderTextTextLineCostCentreInternalOrderGLAccountRepCountryVATRegistrationNoProfitCentreWBSElementWithholdingtaxTypeWithholdingtaxcodeWithholdingtaxbaseamount</Value>
    </Setting>
    <Setting Name="POFileHeader" Type="System.String" Scope="Application">
      <Value Profile="(Default)">IdentifierCurrencyExchangeRatePaymentTermCodeRepCountryVATRegistrationNoCaseIdPaymentMethodPaymentReferenceSCBIndicatorAPOCDocumentTypeERPDocumentTypeISRNumberISRReferenceInvoiceReferenceNumberInvoiceReferenceFiscalYearVendorAssignmentCompanyCodeVendorNumberDocumentNoDocumentDatePostingDateBaselineDateTaxReportingDateUnplannedDeliveryCostVendorHeaderTextDocumentHeaderTextAccountTypeInvoiceLineNumberLineQuantityPONumberPOLineNumberGRNumberLineAmountVATTaxCodeVATRateVATAmountTextLineTotalAmountWithholdingtaxTypeWithholdingtaxcodeWithholdingtaxbaseamountGLAccountCostCentreInternalOrderProfitCentreWBSElementMaterialNumberPlantID</Value>
    </Setting>
    <Setting Name="AzurePOPath" Type="System.String" Scope="Application">
      <Value Profile="(Default)"><company>/Outbound/PO/Ready/</Value>
    </Setting>
    <Setting Name="AzureNonPOPath" Type="System.String" Scope="Application">
      <Value Profile="(Default)"><company>/Outbound/NonPO/Ready/</Value>
    </Setting>
    <Setting Name="AzureNonPOCorruptPath" Type="System.String" Scope="Application">
      <Value Profile="(Default)"><company>/Outbound/NonPO/Corrupt/</Value>
    </Setting>
    <Setting Name="AzurePOCorruptPath" Type="System.String" Scope="Application">
      <Value Profile="(Default)"><company>/Outbound/PO/Corrupt/</Value>
    </Setting>
    <Setting Name="AzureNonPOProcessedPath" Type="System.String" Scope="Application">
      <Value Profile="(Default)"><company>/Outbound/NonPO/Processed/</Value>
    </Setting>
    <Setting Name="AzureUploadInvoiceIndexPath" Type="System.String" Scope="Application">
      <Value Profile="(Default)"><company>/Inbound/InvoiceIndexData/Ready/</Value>
    </Setting>
    <Setting Name="AzureUploadInvoiceAckPath" Type="System.String" Scope="Application">
      <Value Profile="(Default)"><company>/Inbound/InvoiceAcknowledgement/Ready/</Value>
    </Setting>
    <Setting Name="AzurePOProcessedPath" Type="System.String" Scope="Application">
      <Value Profile="(Default)"><company>/Outbound/PO/Processed/</Value>
    </Setting>
    <Setting Name="PostCertificateHash" Type="System.String" Scope="Application">
      <Value Profile="(Default)"><hash></Value>
    </Setting>
    <Setting Name="ArchiveOutputPath" Type="System.String" Scope="Application">
      <Value Profile="(Default)">\\<servername>\EpicorData\APOC\Archive</Value>
    </Setting>
    <Setting Name="PPVGLAccount" Type="System.String" Scope="Application">
      <Value Profile="(Default)"><GLAccount></Value>
    </Setting>
    <Setting Name="AzureConnection" Type="System.String" Scope="Application">
      <Value Profile="(Default)"><connect>
</Value>
    </Setting>
    <Setting Name="AzureConnectionPilot" Type="System.String" Scope="Application">
      <Value Profile="(Default)"><connect>
</Value>
    </Setting>
    <Setting Name="AppServerURLPilot" Type="System.String" Scope="Application">
      <Value Profile="(Default)"><serverURL>/EpicorERPPilot/api/v2/efx/KEN/PostAPInvoice/PostAPGroup/</Value>
    </Setting>
    <Setting Name="APIKeyPilot" Type="System.String" Scope="Application">
      <Value Profile="(Default)"><APIKey></Value>
    </Setting>
    <Setting Name="APOCEmailAddress" Type="System.String" Scope="Application">
      <Value Profile="(Default)"><email1></Value>
    </Setting>
  </Settings>
</SettingsFile>

I’m using the APIKeyPilot value in the program at the moment, and verified it’s correct. How can I get around the SSL issue? I have the certificate for the AppPoolHost installed on my system.

are you reading it from that settings file if so you are doubling up on your https it should be just

server.tld.com // not https://server.tld.com if you are reading this value in for the EpicorRest.Host
1 Like

Value I’m using for the AppPoolHost property is just the FQDN of the server, no HTTPS:// on it. The AppPoolInstance value should be the app pool name, right? not like “net.tcp:///”, correct?

Correct

Then this doesn’t make any sense… I’m doind exactly what you’ve suggested and still getting the error. I’ even have the IgnoreCertErrors property = true.

can you access the calls through POSTMan?

Do I need to do the CreateBearerToken() call before a Post function call?

You don’t need to do CreateBearer at all unless you need a bearer token. That was just an example.

Can you successfully make the call using Postman from that same machine?

Yes… That’s what is so crazy about this, Postman works calling the function

You are setting something incorrectly in your code I can’t see that from here without running your code for you i’m not sure how to help you.

I gave you working code that runs in that github project. Does that work for you? if you change the settings?

I’ve been trying the RestExample project over the past few minutes with my parameters and trying to run the zCustomers01 BAQ, then one of my own. I’m getting back a 404 error in the ResponseBody:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>404 - File or directory not found.</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;} 
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;} 
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} 
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
-->
</style>
</head>
<body>
<div id="header"><h1>Server Error</h1></div>
<div id="content">
 <div class="content-container"><fieldset>
  <h2>404 - File or directory not found.</h2>
  <h3>The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.</h3>
 </fieldset></div>
</div>
</body>
</html>

Sorry, I misstated the original BAQ in the project, it’s zCustomer01…

Jose, could any of these issues I’m having be related to the Epicor version I’m on? We’re still on 10.2.700.21. Also, I haven’t tried PostMan recently, I’ve been using the V2 Help pages to run the function.

No this is not related to your version of Epicor. I thought you said you tried postman, see if you can get postman working.

Jose, I apologize for taking so long to get back to you, I was on vacation Friday thru Monday. I got Postman working with the help of a colleague, but I’m still getting a 401 error when attempting the POST call:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<title>401 - Unauthorized: Access is denied due to invalid credentials.</title>
	<style type="text/css">
		<!--
		body {
			margin: 0;
…				<h3>You do not have permission to view this directory or page using the credentials that you supplied.
				</h3>
			</fieldset>
		</div>
	</div>
</body>

</html>