Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Business Central forum

How to consume REST API Url in Microsoft D365 Business Central?

(0) ShareShare
ReportReport
Posted on by 246

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.

 

 

Categories:
  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to consume REST API Url in Microsoft D365 Business Central?

    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 

     trigger OnOpenPage();
        begin
            //apiRequestQuery := '{"timestamp": "","LeaveAccrual": "test","InsertedOn": "1753-01-01T00:00:00","InsertedBy": "","UpdatedOn": "1753-01-01T00:00:00","UpdatedBy": "","C_systemId": "c1eef8af-c25e-ea11-b7f0-90b11c65cdee"}';
            //webApi.POST_Request('localhost:63273/.../LeaveAccrual', apiRequestQuery);
            //webApi.GET_Request('localhost:63273/.../LeaveAccrual');
        end;
  • Suggested answer
    Stefano Demiliani Profile Picture
    Stefano Demiliani 37,162 Most Valuable Professional on at
    RE: How to consume REST API Url in Microsoft D365 Business Central?

    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;

  • NeoMatrix Profile Picture
    NeoMatrix 246 on at
    RE: How to consume REST API Url in Microsoft D365 Business Central?

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

  • Suggested answer
    Stefano Demiliani Profile Picture
    Stefano Demiliani 37,162 Most Valuable Professional on at
    RE: How to consume REST API Url in Microsoft D365 Business Central?

    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();

           }        

       }

    }

  • NeoMatrix Profile Picture
    NeoMatrix 246 on at
    RE: How to consume REST API Url in Microsoft D365 Business Central?

    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.

  • Suggested answer
    Stefano Demiliani Profile Picture
    Stefano Demiliani 37,162 Most Valuable Professional on at
    RE: How to consume REST API Url in Microsoft D365 Business Central?

    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]

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,321 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans