Monsieur Winner

MS D365FO || Monsieur D365FO

Technical

Consume JSON API x++ in D365FO using RestSharp

https://drive.google.com/file/d/1kvGtSiLiq2uu152tkkrCnf80ZsLA_RYc/view
https://drive.google.com/file/d/1WBvSWTataAixLTtm7Cwi1vcb2YbxVPfR/view

Integrating an Enterprise resource planning application like Dynamics 365 Finance and Supply chain is now a fully part of a developer major tasks.

In my previous post Consume API in Dynamics 365 Finance and Operations , I demonstrated how we can consume a JSON API through a C# project and reference it in X++.

“All roads lead to Rome”, they say. We can use the RestSharp open source library to achieve same result.

What’s RestSharp

RestSharp is a free, open-source library that allows developers to send HTTP requests and process responses in .NET applications. It’s a wrapper around HttpClient, but adds additional functionality.

What does RestSharp do?

  • Serializes payloads to JSON and XML
  • Configures content headers
  • Handles responses from remote endpoints
  • Deserializes JSON and XML responses
  • Adds parameters to requests, such as queries, forms, and headers
  • Interfaces with public APIs
  • Accesses data without dealing with raw HTTP requests

 

Can we get into the main deal please?

Yes, my lord. We will be connecting to WordPress API to get posts from this very blog https://blog.monsieurwinner.com/.

WordPress allows you to pull articles(posts) with a GET request via REST API and return the value in a JSON format.

URL = siteURL/wp-json/wp/v2/posts.  So our request URL will be https://blog.monsieurwinner.com/wp-json/wp/v2/posts.

No authentication is required to retrieve collection of posts. Our url in the web browser, or even in postman will return –>

consume JSON API in Dynamics 365FO.JPG

 

WordPress return the post as a list  (JSON Array Literals).

Call REST API in Dynamics 365fo using RestSharp.

  • Firstly , you need to download the RestSharp dlls.
  • Add the dll files as reference to the X++ Project
  • Create a Runnable class and import the name space with the using keyword.
using RestSharp;
using RestSharp.Authenticators;
using RestSharp.Extensions.ResponseExtensions;
using Newtonsoft.Json.JsonSerializer;
using Newtonsoft.Json.Linq;
using RestSharp.Serializers.NewtonsoftJson.JsonNetSerializer;
internal final class ConsumeAPI
{

}

Let’s create method connectToWordpress()

 

    public void connectToWordpress()
    {
        Notes jsonBody, jsonResponse, accessToken;
        str contentType = "application/json-patch+json"; //You should use labelId or #Macro. - I know :)
        str accept = "text/plain";


        var clients = new RestClient("https://blog.monsieurwinner.com/wp-json/wp/v2/posts");//The URL here should be in a parm t
        clients.Timeout = -1;

        var apiRequest = new RestRequest(Method::GET);
        apiRequest.AddHeader("accept", accept);
        apiRequest.AddHeader("Content-Type", contentType);
        apiRequest.AddParameter("application/json", jsonBody,  ParameterType::RequestBody);

        IRestResponse response = clients.Execute(apiRequest);
        jsonResponse = response.Content;
        Info(strFmt("Success: %1",jsonResponse));
    }

In the request above, on lines 140-142, we are defining the request, header and body of the request. Here, the body is empty.

On line 144, IRestResponse, an interface in the RestSharp DLL return response data from the API.

Komi Siabi

Komi Siabi is a Bilingual D365FO Solution architect who loves sharing his knowledge as he works on Both Francophone and Anglophone projects around the globe. He enjoys doing some tiktok videos in his leisure time.

Leave a Reply