X++ code to call REST API and get response

 This code will help you to make get request to an API and gets response.

class BASRetailCommonWebAPIJob

{

    public static void main(Args _args)

    {

        System.Exception        ex;

        try

        {

            RetailWebRequest    webRequest;

            RetailWebResponse   response;

            RetailCommonWebAPI  webApi;

            str                 rawResponse, ref;

            Map                 responseData;


            webApi = RetailCommonWebAPI::construct();


            //URL to get data

            str tenantId = xInfo::serialNo();

            webRequest = RetailWebRequest::newUrl(@"URL="+tenantId);

            webRequest.parmMethod("GET");


            response = webApi.getResponse(webRequest);


            if(response.parmHttpStatus() != 200)

            {

                throw error("No data is returned");

            }


            rawResponse = substr(response.parmData(),2,(strlen(response.parmData())-2));

            Info(rawResponse);

        }

        catch

        {

            ex = CLRInterop::getLastException().GetBaseException();

            error(ex.get_Message());

        }

    }



Comments

Popular posts from this blog

How to Create a wizard in x++ d365

how to post trade agreement journals automatically using x++ code

X++ code to CREATE AND POST A TRADE AGREEMENT IN MICROSOFT DYNAMICS 365 in D365 F & O