Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans