Wednesday, January 17, 2024

Send Http request with JSON payload using X++

Public class BizNodeIntegration_Custom
{
    public static void CreditRatingUpdate (CustTable    _custTable)
    {
        URL  url = strLTrim(@'https://test-integrations-syncoperations.azurewebsites.net/api/companyRating?code=fgfgfgfgfgfg');
        //URL     url  = SysAppUtilities_Custom::getKeyVaultSecret(SysAppUtilities_Custom::IntegrationsFunctionsBaseUrl) + '/api/creditRating?code=' + SysAppUtilities_Custom::getKeyVaultSecret(SysAppUtilities_Custom::IntegrationsFunctionAppMasterKey);
        System.IO.StringWriter          stringWriter;
        Newtonsoft.Json.JsonTextWriter  jsonWriter;
        System.Byte[]         bytes;
        System.Text.Encoding     utf8;

        stringWriter       = new System.IO.StringWriter();
        jsonWriter         = new Newtonsoft.Json.JsonTextWriter(stringWriter);

        str jsonString = "";
        jsonWriter.WriteStartObject();
 
        jsonWriter.WritePropertyName("RegistrationNumber");
        jsonWriter.WriteValue(_custTable.registrationNumber());
 
        jsonWriter.WritePropertyName("CountryCode");
        jsonWriter.WriteValue(strDel(_custTable.VATNum,3,strLen(_custTable.VATNum)));

        jsonWriter.WriteEndObject();
        jsonString = stringWriter.ToString();

        str method = 'POST';
        str contentType = @'application/json';
        RetailWebRequest webRequest = RetailWebRequest::newUrl(url);

        utf8 = System.Text.Encoding::get_UTF8();

        bytes = utf8.GetBytes(jsonString);

        webRequest.parmMethod(method);
        webRequest.parmContentType(contentType);
        webRequest.setContentBytes(bytes);

        RetailCommonWebAPI webApi = RetailCommonWebAPI::construct();
        RetailWebResponse webResponse = webApi.getResponse(webRequest);
        str responseData = webResponse.parmData();

        info(responseData);

        if (webResponse.parmHttpStatus() == 200)
        {
            Map responseMap = RetailCommonWebAPI::getMapFromJsonString(responseData);
            MapEnumerator       mapEnumerator;
            mapEnumerator = responseMap.getEnumerator();
            while (mapEnumerator.moveNext())
            {
                switch (mapEnumerator.currentKey())
                {
                    case "payload"     :
                        container   con = mapEnumerator.currentValue();
                        CustomerCreditInsuranceUpdate_Custom::updateCustomerCreditInsurance(conPeek(con,conFind(con,'companyRating') + 1),_custTable.registrationNumber());
                        break;

                    default:
                        break;
                }
            }
        }
        else
        {
            info(responseData);
        }
    }

}

No comments:

Post a Comment

Table browser URL in D365FO

Critical Thinking icon icon by Icons8