Announcements
Hi, All.
I want to Consume Customer Details Rest API Url in MSFT D365 Business Central for PUT, POST and GET Request. [Ther is One form of Java Application of Customer details when a user enters the details of the customer then that Customer details also insert in Customer form in Business Central.]
Any help greatly appreciated.
codeunit 50100 TestWebApi
{
trigger OnRun()
begin
end;
procedure GET_Request(uri: Text) responseText: Text
begin
//json := StrSubstNo('localhost:63273/.../LeaveAccrual');
json := StrSubstNo(uri);
if client.Get(json, Response) then begin
Response.Content.ReadAs(json);
Message(json);
exit(json);
end;
end;
procedure POST_Request(uri: Text; _queryObj: Text) responseText: Text;
var
client: HttpClient;
request: HttpRequestMessage;
response: HttpResponseMessage;
contentHeaders: HttpHeaders;
content: HttpContent;
begin
// Add the payload to the content
content.WriteFrom(_queryObj);
// Retrieve the contentHeaders associated with the content
content.GetHeaders(contentHeaders);
contentHeaders.Clear();
contentHeaders.Add('Content-Type', 'application/json');
// Assigning content to request.Content will actually create a copy of the content and assign it.
// After this line, modifying the content variable or its associated headers will not reflect in
// the content associated with the request message
request.Content := content;
request.SetRequestUri(uri);
request.Method := 'POST';
client.Send(request, response);
// Read the response content as json.
response.Content().ReadAs(responseText);
end;
var
Client: HttpClient;
Response: HttpResponseMessage;
json: Text;
_httpContent: HttpContent;
jsonObj: JsonObject;
}
if it is helpfull to you.
there is Get from Rest api & Post from api.
-------------------------------------------
when you call api from any page
Do you want to consume an external APIs directly from D365BC (so via AL code)?
If so, you have to use HttpClient class and send a GET request to your API. Then you need to parse the JSON response.
Something like:
var
Client: HttpClient;
Response: HttpResponseMessage;
json: Text;
jsonObj: JsonObject;
begin
client.Get(FunctionURL + YourParameters, Response);
//Reads the response content from the Azure Function
Response.Content().ReadAs(json);
if not jsonObj.ReadFrom(json) then
Error(InvalidResponseError);
//Here you need to parse jsonObj (the JSON of your response)
end;
Thanks for the Reply.
but where I write above code then How it consume in Business Central. I didn't understand.
Could you provide step by step procedure if it Possible?
This is My Url "http://localhost:52944/api/home" and It Return Like Data
"[
{
"EmpID": 101,
"Name": "AAA",
"City": "Delhi"
},
{
"EmpID": 102,
"Name": "bbb",
"City": "Mubai"
}
]
I want to consume above URL in Business central [I used On prime BC].
You’ve to send an HTTP request to the API endpoint and read the response.
This is a C# sample that calls a generic API, I think in Java is quite the same:
namespace HttpClientDemo
{
class Program
{
static void Main(string[] args)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("[View:http://localhost:60464/api/]");
//HTTP GET
var responseTask = client.GetAsync("student");
responseTask.Wait();
var result = responseTask.Result;
if (result.IsSuccessStatusCode)
{
var readTask = result.Content.ReadAsAsync<Student[]>();
readTask.Wait();
var students = readTask.Result;
foreach (var student in students)
{
Console.WriteLine(student.Name);
}
}
}
Console.ReadLine();
}
}
}
Could you elaborate related with coding such as how to send HTTTP Request and how to Parse URL in D365 BC. Because I did'nt work with REST API Services.
Using REST APIs in D365BC is just like consuming REST APIs on other web applications.
You need to send an HTTP request to the API endpoint and then reading the response content.
All details here:
[View:https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-develop-connect-apps]
André Arnaud de Cal...
294,217
Super User 2025 Season 1
Martin Dráb
232,978
Most Valuable Professional
nmaenpaa
101,158
Moderator