web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

GET/POST API request gives error

(1) ShareShare
ReportReport
Posted on by 41
Hello all,
 
I have a scenario to get/post api httprequest with 3 headers (client key, client secret, api key).  I tried with the below code.
 
and i am not sure how to pass the headers with 3 component. It gives me the error /The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel/. 
 
After debugging, the error is being thrown from RetailCommonWebAPI(getResponse) method.  
class TestRequest
{
    public static void main(Args _args)
    {
        //Creaates a request
        RetailWebRequest webRequest = TestRequest::getSalesRequest();
        RetailCommonWebAPI webApi = RetailCommonWebAPI::construct();
        RetailWebResponse webResponse = webApi.getResponse(webRequest);
        str responseData = webResponse.parmData();
         
        if (webResponse.parmHttpStatus() == 200)
        {
            //logic           
        }        
    }
    public static RetailWebRequest getSalesRequest()
    {
        URL url = strLTrim(*********);     
 
        str method = 'POST';
        str contentType = @'application/json';
        str requestBody = '{/Value0/: ####, /Value1/: ####}';
 
        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
 
        RetailWebRequest webRequest = RetailWebRequest::newUrl(url);

        System.Net.WebHeaderCollection headers = new System.Net.WebHeaderCollection();
        headers.add(/Client_key/, /****/);
        headers.add(/Client_Secret/, /********/);
        headers.add(/API_Key/, /*************/);
        
        webRequest.parmMethod(method);
        webRequest.parmHeader(headers);
        webRequest.parmContentType(contentType);
        webRequest.setContentBytes(encoding.GetBytes(requestBody));
 
        return webRequest;
    }
}
 
Please let me know how to call header and any fix for the error message. 
I have the same question (0)
  • Suggested answer
    Layan Jwei Profile Picture
    8,112 Super User 2025 Season 2 on at
    Hi Monikasree_GM
     
    This was asked by someone in this thread: https://community.dynamics.com/forums/thread/details/?threadid=b5ed110c-b448-ee11-be6d-00224827ed84#:~:text=To%20resolve%20this%20in%20D365FO,API%20provider%20for%20specific%20requirements.

    I will repeat my answer here:
    ​​​​​​​
    1. Did you try to call this URL from postman and it worked?
     
    2. To double check if it's an issue from the code or not, then please try to call a public API url in your x++ code instead of the current one.
    ​​​​​​​    Choose one of the public APIs here and let us know (https://documenter.getpostman.com/view/8854915/Szf7znEe).
        If it works, then the issue is not from your code and might be related to firewall/security or maybe you will need to contact the API provider. However, if it doesn't work as well,
        then there might be sth with the code. so please confirm.

    Thanks,
    Layan Jweihan
    Please mark the answer as "Verified" if it solved your question. In order to help others as well
  • Verified answer
    Monikasree GM Profile Picture
    41 on at
    Hello @Layan Jwei ,
     
    Thank you for the information.
     
    1. Postman worked
     
    2. I can access public URL, issue was with the one of the certificate from third party service.
     
    I have used the below code to bypass the validation as a workaround, Alternatively, we can install the certificate.
     
     System.Net.Security.RemoteCertificateValidationCallback callBack;
     callBack += eventhandler(BECUnikGETRequest::validateCert);
    System.Net.ServicePointManager::ServerCertificateValidationCallback = callBack;
     
       public static boolean validateCert(System.Object sender,
                                    System.Security.Cryptography.X509Certificates.X509Certificate cert,
                                    System.Security.Cryptography.X509Certificates.X509Chain chain,
                                    System.Net.Security.SslPolicyErrors errors)
        {
            return true;
        }
     
    Regards,
    Monika

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 544 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 450 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 250 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans