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. 
  • Verified answer
    Monikasree GM Profile Picture
    41 on at
    GET/POST API request gives error
    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
  • Suggested answer
    Layan Jwei Profile Picture
    7,725 Super User 2025 Season 1 on at
    GET/POST API request gives error
    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

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,245 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,927 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans