Hi Readers,
In this article will see how we can create connection with external application using HTTP and read the Json data & store it in business central.
In this scenario, if i will enter the ID then it will fill all the related details by getting information from third party application.
Step-(1) Firstly i have created the table and call the procedure on OnValidate trigger.
table 50102 "User Details API"
{
DataClassification = ToBeClassified;
fields
{
field(1; ID; Integer)
{
DataClassification = ToBeClassified;
trigger OnValidate()
var
JsonReadAndWrite_CU: Codeunit "Json Write";
begin
JsonReadAndWrite_CU.JsonReadAndWrite(Rec);
end;
}
field(2; Name; Text[50])
{
DataClassification = ToBeClassified;
}
field(3; "User Name"; Text[50])
{
DataClassification = ToBeClassified;
}
field(4; "Email"; Text[50])
{
DataClassification = ToBeClassified;
}
field(5; "Street"; Text[50])
{
DataClassification = ToBeClassified;
}
field(6; "Suite"; Text[50])
{
DataClassification = ToBeClassified;
}
}
keys
{
key(PK; ID)
{
Clustered = true;
}
}
}
Step-(2) Then i have created the page which basically used to interact with user & see the output.
page 50103 "User Details API"
{
PageType = List;
ApplicationArea = All;
UsageCategory = Lists;
SourceTable = "User Details API";
layout
{
area(Content)
{
repeater(List)
{
field(ID; ID) { ApplicationArea = All; }
field(Name; Name) { ApplicationArea = All; }
field("User Name"; "User Name") { ApplicationArea = All; }
field(Email; Email) { ApplicationArea = All; }
field(Street; Street) { ApplicationArea = All; }
field(Suite; Suite) { ApplicationArea = All; }
}
}
}
}
Step-(3) Now finally i have create a function which makes the connection using HTTP and get data from external application & store in Business Central table.
Procedure JsonReadAndWrite(Var UserDetailsAPI: Record "User Details API")
var
UrlPath: Text;
HttpClientVar: HttpClient;
HttpResponseVar: HttpResponseMessage;
HttpContentVar: HttpContent;
Result: Text;
//Method2
HttpRequest: HttpRequestMessage;
Output: Text;
JObject: JsonObject;
JToken: JsonToken;
NewJObject: JsonObject;
begin
UrlPath := 'https://jsonplaceholder.typicode.com/users/' + Format(UserDetailsAPI.ID);
HttpClientVar.Get(UrlPath, HttpResponseVar);
if HttpResponseVar.IsSuccessStatusCode then begin
HttpContentVar := HttpResponseVar.Content;
HttpContentVar.ReadAs(Result);
// Message(Result);
//To store the content in Json Object
JObject.ReadFrom(Result);
JObject.Get('name', JToken);
UserDetailsAPI.Name := JToken.AsValue().AsText();
JObject.Get('email', JToken);
UserDetailsAPI.Email := JToken.AsValue().AsText();
JObject.Get('username', JToken);
UserDetailsAPI."User Name" := JToken.AsValue().AsText();
JObject.Get('address', JToken);
if JToken.IsObject then begin
JToken.WriteTo(Output);
NewJObject.ReadFrom(Output);
NewJObject.Get('street', JToken);
UserDetailsAPI.Street := JToken.AsValue().AsText();
NewJObject.Get('suite', JToken);
UserDetailsAPI.Suite := JToken.AsValue().AsText();
end else
Message('Json Token is not found as Json Object.');
end else
Message('Connection not Established..Status Code is %1 & Reason is %2', HttpResponseVar.HttpStatusCode, HttpResponseVar.ReasonPhrase);
end;
Output:-
Thanks for Reading!!
Comments
Post a Comment