Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX forum
Answered

Need to call API. First to get response in form of token and then send some data to API to get response in True/False.

Posted on by 502

Hi everyone,

I need to get response from an API call which will send me a token. And onse token received, I need to call API to get response.

I have already tested the API CALL from power automate and it is working fine.

Now I need help in getting this process to be done from a class call. Could you please help me with the code which I can use to get this done?

Regards

Siddhant Singh

  • Verified answer
    Urvashi Kumari Profile Picture
    Urvashi Kumari 13 on at
    RE: Need to call API. First to get response in form of token and then send some data to API to get response in True/False.

    using System.Net;
    using System.Net.Http;
    
     
    
    class SST_APICall
    {
        public static void main(Args _args)
        {
            System.Net.HttpWebRequest       request;
            System.Net.HttpWebResponse      response;
            CLRObject                       clrObj;
            System.Exception                ex;
            System.Net.WebHeaderCollection  httpHeader;
            System.IO.Stream                requestStream, responseStream;
            SST_APITable                    table1;
            System.IO.StreamWriter          streamWriter;
            System.Net.Cache.HttpRequestCachePolicy policy;
            //System.Net.Cache.HttpRequestCacheLevel cacheLevel;
            //policy = new System.Net.Cache.HttpRequestCachePolicy(cacheLevel);
    
     
    
            str url = "http://yourhost.net/service";
            str clientId = "";
            str secretId = "";
    
     
    
    
            select firstonly1 crosscompany table1;
            httpHeader = new System.Net.WebHeaderCollection();
            new InteropPermission(InteropKind::ClrInterop).assert();
            clrObj = System.Net.WebRequest::Create("https:XXXXX");
    
     
    
            System.Text.Encoding encoding = System.Text.Encoding::UTF8;
            System.Byte[] bytes = encoding.GetBytes(clientId   ":"   secretId);
            System.String base64 = System.Convert::ToBase64String(bytes);
    
     
    
            System.String headerKey = "Authorization";
            System.String headerValue = "Basic "   base64;
            //System.Net.WebRequest request = System.Net.WebRequest::Create(url);
            System.Net.WebHeaderCollection headers = request.Headers;
            headers.Add(headerKey,headerValue);
    
     
    
            request = clrObj;
            // adding headers
            /*headers.Add("Grant", "XXXXX");
            headers.Add("owner_id", "XXXXX");
            headers.Add("gstin", "XXXXX");*/
    
     
    
            request.set_Headers(headers);
            request.Method = "POST";
            request.Accept = "application/json";
            request.ContentType = "application/x-www-form-urlencoded";
    
            requestStream = request.GetRequestStream();
    
     
    
            streamWriter = new System.IO.StreamWriter(request.GetRequestStream());
            streamWriter.Write(table1.FieldString1); // writing JSON
            streamWriter.Flush();
            streamWriter.Close();
    
     
    
            response = request.GetResponse();
            System.IO.StreamReader streamRead = new System.IO.StreamReader(response.GetResponseStream());
            info(streamRead.ReadToEnd());
        }
    
     
    
    }
    
    has context menu

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,696 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,490 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans