In this post I'll be showing you how to send a GET call from dynamics 365 F&O system and the return format is in JSON.
Created a simple x++ job below. Please change according to your requirement.
class TestExternalCall_GET_JSON
{
/// <summary>
/// Runs the class with the specified arguments.
/// </summary>
/// <param name = "_args">The specified arguments.</param>
public static void main(Args _args)
{
int find;
str url,aosUri,activeDirectoryTenant;
str activeDirectoryClientAppId;
str activeDirectoryClientAppSecret;
str postData,activeDirectoryResource;
str aadClientAppSecret,oAuthHeader;
str returnValue,jsonString,jsondszstr;
System.Net.HttpWebRequest request;
System.Net.HttpWebResponse response;
System.Byte[] byteArray;
System.IO.Stream dataStream;
System.IO.StreamReader streamRead;
System.IO.StreamWriter streamWrite;
System.Net.ServicePoint servicePoint;
System.Net.ServicePointManager servicePointmgr;
System.Net.HttpVersion version;
CLRObject clrObj;
Newtonsoft.Json.JsonReader reader;
System.Text.Encoding utf8;
Counter countCounter;
Object obj;
Map data;
System.Byte[] byteArraynew;
System.Net.WebHeaderCollection headers = new System.Net.WebHeaderCollection();
str byteStr = strfmt('%1:%2', "USERNAME", "PASSWORD");
new InteropPermission(InteropKind::ClrInterop).assert();
headers = new System.Net.WebHeaderCollection();
url = "Paste endpoint URL here";
clrObj = System.Net.WebRequest::Create(url);
request = clrObj;
request.set_Method("GET");
request.set_KeepAlive(true);
request.set_ContentType("application/json");
utf8 = System.Text.Encoding::get_UTF8();
byteArraynew = utf8.GetBytes(byteStr);
byteStr = System.Convert::ToBase64String(byteArraynew);
headers.Add("Authorization", 'Basic ' + byteStr);
request.set_Headers(headers);
servicePoint = request.get_ServicePoint();
System.Net.ServicePointManager::set_Expect100Continue(false);
System.Net.ServicePointManager::set_SecurityProtocol(System.Net.SecurityProtocolType::Tls12);
response = request.GetResponse();
dataStream = response.GetResponseStream();
streamRead = new System.IO.StreamReader(dataStream);
jsonString = streamRead.ReadToEnd();
info(strFmt("%1",jsonString));
dataStream.Close();
response.Close();
}
}
In my next post I'll show you how to use OAUTH in GET call instead of normal authorization.
@Rahul
Created a simple x++ job below. Please change according to your requirement.
class TestExternalCall_GET_JSON
{
/// <summary>
/// Runs the class with the specified arguments.
/// </summary>
/// <param name = "_args">The specified arguments.</param>
public static void main(Args _args)
{
int find;
str url,aosUri,activeDirectoryTenant;
str activeDirectoryClientAppId;
str activeDirectoryClientAppSecret;
str postData,activeDirectoryResource;
str aadClientAppSecret,oAuthHeader;
str returnValue,jsonString,jsondszstr;
System.Net.HttpWebRequest request;
System.Net.HttpWebResponse response;
System.Byte[] byteArray;
System.IO.Stream dataStream;
System.IO.StreamReader streamRead;
System.IO.StreamWriter streamWrite;
System.Net.ServicePoint servicePoint;
System.Net.ServicePointManager servicePointmgr;
System.Net.HttpVersion version;
CLRObject clrObj;
Newtonsoft.Json.JsonReader reader;
System.Text.Encoding utf8;
Counter countCounter;
Object obj;
Map data;
System.Byte[] byteArraynew;
System.Net.WebHeaderCollection headers = new System.Net.WebHeaderCollection();
str byteStr = strfmt('%1:%2', "USERNAME", "PASSWORD");
new InteropPermission(InteropKind::ClrInterop).assert();
headers = new System.Net.WebHeaderCollection();
url = "Paste endpoint URL here";
clrObj = System.Net.WebRequest::Create(url);
request = clrObj;
request.set_Method("GET");
request.set_KeepAlive(true);
request.set_ContentType("application/json");
utf8 = System.Text.Encoding::get_UTF8();
byteArraynew = utf8.GetBytes(byteStr);
byteStr = System.Convert::ToBase64String(byteArraynew);
headers.Add("Authorization", 'Basic ' + byteStr);
request.set_Headers(headers);
servicePoint = request.get_ServicePoint();
System.Net.ServicePointManager::set_Expect100Continue(false);
System.Net.ServicePointManager::set_SecurityProtocol(System.Net.SecurityProtocolType::Tls12);
response = request.GetResponse();
dataStream = response.GetResponseStream();
streamRead = new System.IO.StreamReader(dataStream);
jsonString = streamRead.ReadToEnd();
info(strFmt("%1",jsonString));
dataStream.Close();
response.Close();
}
}
In my next post I'll show you how to use OAUTH in GET call instead of normal authorization.
@Rahul
Thank you
ReplyDeletethanks bro
ReplyDeleteI feel there is a need to provide some more information about REST API and its uses alongwith complexities.
ReplyDeleteSQL Server Load Rest API
how to use token based api authentication
ReplyDeleteWhen you use a genuine service, you will be able to provide instructions, share materials and choose the formatting style. outsourcing services
ReplyDeleteHi, did you ever create a blog on doing this but using Oauth at the authentication?
ReplyDelete