
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
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