Streamlining Outbound Integrations: Crafting a Code Snippet for Non-Authentication APIs



Greetings, AX/D365 finance enthusiasts! Today, I'm excited to share with you a code snippet that I recently crafted for a straightforward outbound integration. I want to clarify upfront that this snippet prioritizes the core functionality of the integration, and authentication aspects are not included.

During our project involving an outbound integration with an HTTPS API, we encountered a scenario where the client didn't furnish authentication details such as OAuth or a bearer token. Instead, they solely provided a JSON body in x-www-form-urlencoded format.

Initially, I developed the code in C#, and later translated it into X++. Below, you'll find the snippet, which I hope will come in handy for you in similar situations down the road.


class D365OutboundIntegration
{
    static void main(Args _args)
    {
        str outboundUrl = "http://OutboundURL.com";
        System.Net.WebClient client = new System.Net.WebClient();

        System.Collections.Specialized.NameValueCollection formData = new System.Collections.Specialized.NameValueCollection();
        formData.add("serviceIntKey", "fg34IsMIFlaai23gcTg0Tmsfds66wtw =");
        formData.add("updateDirection", "toThirdParty");
        formData.add("ProjIdFrom", "PROJ-009");
        formData.add("projectIdTo", "ThirdPartyProjectID");

        try
        {
            // Add the Content-Type header
            client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

            // Upload the form data
            byte[] responseBytes = client.UploadValues(outboundUrl, formData);

            // Convert the response bytes to a string
            str responseData = System.Text.Encoding::UTF8.GetString(responseBytes);

            // Display the response data
            info(responseData);
        }
        catch
        {
            error("An error occurred while sending the request.");
        }
    }
}

 


No comments

Powered by Blogger.