web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

I am trying to consume third party api in business where the content type - is application/x-www-form-urlencoded and i am getting error in business central that format is invalid whereas i am able to consume it in postman for get and post request .

(0) ShareShare
ReportReport
Posted on by 143

Can anyone please share the script for url encoded content type.

I have the same question (0)
  • Suggested answer
    Nitin Verma Profile Picture
    21,708 Moderator on at

    Hi,

    Please share your code.

    Thanks.

  • pooja gund Profile Picture
    143 on at
    action(GetItemInformation)
                {
                    ApplicationArea = All;
                    Caption = 'Get items from stock adjustment API';

                    trigger OnAction()
                    var

                        http_client: HttpClient;
                        http_Responsemsg: HttpResponseMessage;
                        response: Text;
                        //cu: Codeunit TestWebApi;
                        queryObj: Text;
                        contentHeaders: HttpHeaders;
                        content1: HttpContent;
                        request: HttpRequestMessage;
                        jsonObject1: JsonObject;
                        jsonarry: JsonArray;
                        //o: Codeunit "Web Request Helper";
                        // resthelper: Codeunit "REST Helper WLD";
                        jsontextvar: Text;
                        array: JsonArray;
                        url: Text;
                        header: HttpHeaders;
                        jso: JsonValue;
                        jsontextvar1: Text;
                        RequestBody: Text;
                        mycodeunit: Codeunit MyCodeunit3;
                        jsonvar: Text;
                        jsonvar1: Text;

                        wr: Codeunit 1297;
                    begin

                        // jsontextvar := '{"skuCodes":[], "date_from":"09/02/2023 12:32:23","date_to":"15/02/2023 12:32:23","pageNumber":"1"}';


                        // '{"stoNos":[],"FromDate":"09/02/2023 00:00:00","ToDate":"14/02/2023 00:00:00","srcLocation":"HWH","stoType":[""],"pageNumber":"","filterBy":"1"}';

                        jsontextvar := 'date:"",code:""';
                        // jsontextvar := '{"phone":["9654498250"],"email_id":["manisha.raghav7@gmail.com"],"fromDate":"","toDate":"","pageNumber":""}';
                        mycodeunit.MakeRequest('xyz.com', jsontextvar)

                    end;
                }


                // action(GetItemInformation1)
                // {
                //     ApplicationArea = All;
                //     Caption = 'Get items from  popull API ';

                //     trigger OnAction()
                //     var

                //         http_client: HttpClient;
                //         http_Responsemsg: HttpResponseMessage;
                //         response: Text;
                //         //cu: Codeunit TestWebApi;
                //         queryObj: Text;
                //         contentHeaders: HttpHeaders;
                //         content1: HttpContent;
                //         request: HttpRequestMessage;
                //         jsonObject1: JsonObject;
                //         jsonarry: JsonArray;
                //         //o: Codeunit "Web Request Helper";
                //         // resthelper: Codeunit "REST Helper WLD";
                //         jsontextvar: Text;
                //         array: JsonArray;
                //         url: Text;
                //         header: HttpHeaders;
                //         jso: JsonValue;
                //         jsontextvar1: Text;
                //         RequestBody: Text;
                //         mycodeunit: Codeunit MyCodeunit3;
                //         jsonvar: Text;
                //         jsonvar1: Text;

                //         wr: Codeunit 1297;
                //     begin

                //         // jsontextvar := '{"skuCodes":[], "date_from":"09/02/2023 12:32:23","date_to":"15/02/2023 12:32:23","pageNumber":"1"}';


                //         // '{"stoNos":[],"FromDate":"09/02/2023 00:00:00","ToDate":"14/02/2023 00:00:00","srcLocation":"HWH","stoType":[""],"pageNumber":"","filterBy":"1"}';

                //         jsontextvar := '{"poCode": "","status": [],"fromDate": "09/03/2023 00:00:00","toDate": "11/03/2023 00:00:00","pageNumber": "","filterBy": "1"}';
                //         // jsontextvar := '{"phone":["9654498250"],"email_id":["manisha.raghav7@gmail.com"],"fromDate":"","toDate":"","pageNumber":""}';
                //         mycodeunit.MakeRequest('holistique.vineretail.com/.../popull', jsontextvar)

                //     end;
                // }


                action(CreateRecord)
                {

                    ApplicationArea = All;
                    Caption = 'Create items from Rest API';
                    trigger OnAction()
                    var

                        http_client: HttpClient;
                        http_Responsemsg: HttpResponseMessage;
                        response: Text;
                        //cu: Codeunit TestWebApi;
                        queryObj: Text;
                        contentHeaders: HttpHeaders;
                        content1: HttpContent;
                        request: HttpRequestMessage;
                        jsonObject1: JsonObject;
                        jsonarry: JsonArray;
                        //o: Codeunit "Web Request Helper";
                        // resthelper: Codeunit "REST Helper WLD";
                        jsontextvar: Text;
                        array: JsonArray;
                        url: Text;
                        header: HttpHeaders;
                        jso: JsonValue;
                        mycodeunit: Codeunit MyCodeunit3;

                    // possierint: Codeunit 50102;
                    begin
                        // jsonObject1.Add('storeId', '5d4534c8ecbf4c74e43d16cc');
                        // //jsonObject1.Add('orderDay', '27-12-2022');
                        // jsonObject1.Add('orderDay', '09-02-2023');
                        // jsonObject1.Add('Delete', 'false');
                        // jsonObject1.Add('ProductId', 'p123');
                        // jsonObject1.Add('ProductName', 'p123');
                        // jsonObject1.Add('Description', 'test123');
                        // jsonObject1.Add('UnitGroup', 'nos');
                        // jsonObject1.Add('DecimalSupported', '2');
                        // jsonObject1.Add('HSN', '2222222');
                        // jsonObject1.Add('PriceList', 'INR');
                        //jsonObject1.WriteTo(jsontextvar);
                        //RequestBody := 'Add=true&Modify=false&Delete=false&ProductId=p123&ProductName=p123&Description=test123&UnitGroup=nos&DecimalSupported=2&HSN=2222222&PriceList=INR';

                        //mycodeunit.MakeRequestForCreate('erp.possier.com/.../jsonexport, jsontextvar, '5d4534c8ecbf4c74e43d16cc', Today);


                        // possierint.Run();



                    end;


                }

                action(ReadCompnies)
                {
                    ApplicationArea = all;
                    Caption = 'Get Compnies';
                    trigger OnAction()
                    var
                        http_client: HttpClient;
                        http_response: HttpResponseMessage;
                        Request: HttpRequestMessage;
                        responsetext: Text;
                        content: HttpContent;
                        context: Text;

                        t2: Text;
                        result: Text;
                        string: Text;
                        //TextABC: TextConst ENU = 'abc';
                        url: Code[20];
                    begin


                        //t2 := ''+%CRONUS%20Nederland%20BV+%'';
                        //string := ('CRONUS%20Nederland%20BV');
                        //url:='dynamics.compusoftadvisors.com:8048/.../compnies';
                        //result := dynamics.compusoftadvisors.com:8048/.../compnies
                        result := StrSubstNo('%1', 'dynamics.compusoftadvisors.com:8048/.../Company(''abc'')/compnies');
                        //result := 'dynamics.compusoftadvisors.com:8048/.../Company' + string + '/compnies';
                        //context := 'username=navadmin&password=pass@word1';
                        //content.WriteFrom(context);
                        //http_client.UseWindowsAuthentication('navadmin', 'pass@word1', '');
                        http_client.Get(result, http_response);
                        //http_client.Post(result, content, http_response);
                        http_response.Content.ReadAs(responsetext);

                        Message(responsetext);
                    end;
                }
    request code
    codeunit 50126 MyCodeunit3
    {

        procedure MakeRequest(uri: Text; payload: Text) responseText: Text;

        var
            client: HttpClient;
            request: HttpRequestMessage;
            response: HttpResponseMessage;
            contentHeaders: HttpHeaders;
            content: HttpContent;
            skudetails: record 50138;
            tmpString: Text;
            // TypeHelper: Codeunit "Type Helper";
            granttype: text;
            json_Array: JsonArray;
            json_Object: JsonObject;
            json_Object8: JsonObject;
            json_Object1: JsonObject;
            json_Object2: JsonObject;
            json_Object3: JsonObject;
            json_Array18: JsonArray;
            json_Token19: JsonToken;
            json_Array4: JsonArray;
            json_Object5: JsonObject;
            json_Value: JsonValue;
            json_Token: JsonToken;
            PaymentID3: text;
            json_Object21: JsonObject;
            s: Integer;
            json_Token1: JsonToken;
            json_Object18: JsonObject;
            Coundt: Text;
            json_Object19: JsonObject;
            ItemNo: Text[50];
            json_Array5: JsonArray;
            N: Integer;
            h: Integer;
            json_Token15: JsonToken;
            json_Token3: JsonToken;
            json_Token4: JsonToken;
            json_Token2: JsonToken;
            json_Array20: JsonArray;
            json_Object6: JsonObject;
            json_Token18: JsonToken;
            json_Array8: JsonArray;
            json_Token20: JsonToken;
            json_Object4: JsonObject;
            json_Array3: JsonArray;
            valueJToken: JsonToken;
            json_Token6: JsonToken;
            json_Array9: JsonArray;
            r: Integer;
            Z: Integer;
            json_Array7: JsonArray;
            json_Array1: JsonArray;
            json_Token9: JsonToken;
            json_Object7: JsonObject;
            json_Object10: JsonObject;
            json_Array2: JsonArray;
            json_Token7: JsonToken;
            InvoiceNo: Text[50];
            i: Integer;
            j: Integer;
            k: integer;
            O: Integer;
            m: integer;
            p: integer;
            skudetails1: Record skudetails;
            skuList: Text;
        begin
            // Add the payload to the content
            content.WriteFrom(payload);
            Json_object.WriteTo(tmpString);
            content.WriteFrom(tmpString);
            content.ReadAs(tmpString);

            // Retrieve the contentHeaders associated with the content
            content.GetHeaders(contentHeaders);
           
            contentHeaders.Remove('Content-Type');
            contentHeaders.Add('Content-Type', 'application/x-www-form-urlencoded');
       
           
            contentHeaders.Remove('ApiKey');
            client.DefaultRequestHeaders.Add('ApiOwner', 'xxx');
            client.DefaultRequestHeaders.Add('ApiKey', 'xxx');




            // contentHeaders.Add('accept', 'application/json');
            content.GetHeaders(contentHeaders);
            // 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);
            Message(responseText);
            Clear(skuList);
            if json_Token.ReadFrom(responseText) then begin
                if json_Token.IsObject then begin
                    json_Object1 := json_Token.AsObject();
                    if json_Object1.Get('skulist', valueJToken) then begin
                        if valueJToken.IsArray then begin
                            json_Array := valueJToken.AsArray();
                            for i := 0 to json_Array.count - 1 do begin
                                json_Array.Get(i, json_Token);
                                if json_Token.IsObject then begin
                                    json_Object := json_Token.AsObject();
                                    //To get skuList_skucode
                                    if json_Object.Get('skuList_skucode', valueJToken) then begin
                                        if valueJToken.IsValue then begin
                                            if not valueJToken.AsValue().IsNull then begin
                                                skudetails.Init();
                                                skudetails.skuList_skucode := valueJToken.AsValue().AsText();
                                            end;
                                        end;
                                    end;


                                    //to get skuList_skuName
                                    if json_Object.Get('skuList_skuName', valueJToken) then begin
                                        if valueJToken.IsValue then begin
                                            if not valueJToken.AsValue().IsNull then begin
                                                skudetails.skuList_skuName := valueJToken.AsValue().AsText();
                                                // skuList := valueJToken.AsValue().AsText();
                                            end;

                                        end;

                                    end;
                                    //--

                                    //To get skuList_classification
                                    if json_Object.Get('skuList_classification', valueJToken) then begin
                                        if valueJToken.IsValue then begin
                                            if not valueJToken.AsValue().IsNull then begin
                                                skudetails.skuList_classification := valueJToken.AsValue().AsText();
                                            end;
                                        end;
                                    end;


                                    //To get skuList_parentSKU
                                    if json_Object.Get('skuList_parentSKU', valueJToken) then begin
                                        if valueJToken.IsValue then begin
                                            if not valueJToken.AsValue().IsNull then begin
                                                skudetails.skuList_parentSKU := valueJToken.AsValue().AsText();
                                            end;
                                        end;
                                    end;


                                    //To get skuList_desc
                                    if json_Object.Get('skuList_desc', valueJToken) then begin
                                        if valueJToken.IsValue then begin
                                            if not valueJToken.AsValue().IsNull then begin
                                                skudetails.skuList_desc := valueJToken.AsValue().AsText();
                                            end;
                                        end;
                                    end;


                                    //To get "skuList_detailDESC
                                    if json_Object.Get('"skuList_detailDESC', valueJToken) then begin
                                        if valueJToken.IsValue then begin
                                            if not valueJToken.AsValue().IsNull then begin
                                                skudetails.skuList_detailDESC := valueJToken.AsValue().AsText();
                                            end;
                                        end;
                                    end;


                                    //To get skuList_length
                                    if json_Object.Get('skuList_length', valueJToken) then begin
                                        if valueJToken.IsValue then begin
                                            if not valueJToken.AsValue().IsNull then begin
                                                skudetails.skuList_length := valueJToken.AsValue().AsText();
                                            end;
                                        end;
                                    end;


                                    //To get skuList_width
                                    if json_Object.Get('skuList_width', valueJToken) then begin
                                        if valueJToken.IsValue then begin
                                            if not valueJToken.AsValue().IsNull then begin
                                                skudetails.skuList_width := valueJToken.AsValue().AsText();
                                            end;
                                        end;
                                    end;


                                    //To get skuList_weight
                                    if json_Object.Get('skuList_weight', valueJToken) then begin
                                        if valueJToken.IsValue then begin
                                            if not valueJToken.AsValue().IsNull then begin
                                                skudetails.skuList_weight := valueJToken.AsValue().AsText();
                                            end;
                                        end;
                                    end;
  • Suggested answer
    pooja gund Profile Picture
    143 on at

    codeunit 50126 MyCodeunit3

    {

       procedure MakeRequest(uri: Text; payload: Text) responseText: Text;

       var

           client: HttpClient;

           request: HttpRequestMessage;

           response: HttpResponseMessage;

           contentHeaders: HttpHeaders;

           content: HttpContent;

           skudetails: record 50138;

           tmpString: Text;

           // TypeHelper: Codeunit "Type Helper";

           granttype: text;

           json_Array: JsonArray;

           json_Object: JsonObject;

           json_Object8: JsonObject;

           json_Object1: JsonObject;

           json_Object2: JsonObject;

           json_Object3: JsonObject;

           json_Array18: JsonArray;

           json_Token19: JsonToken;

           json_Array4: JsonArray;

           json_Object5: JsonObject;

           json_Value: JsonValue;

           json_Token: JsonToken;

           PaymentID3: text;

           json_Object21: JsonObject;

           s: Integer;

           json_Token1: JsonToken;

           json_Object18: JsonObject;

           Coundt: Text;

           json_Object19: JsonObject;

           ItemNo: Text[50];

           json_Array5: JsonArray;

           N: Integer;

           h: Integer;

           json_Token15: JsonToken;

           json_Token3: JsonToken;

           json_Token4: JsonToken;

           json_Token2: JsonToken;

           json_Array20: JsonArray;

           json_Object6: JsonObject;

           json_Token18: JsonToken;

           json_Array8: JsonArray;

           json_Token20: JsonToken;

           json_Object4: JsonObject;

           json_Array3: JsonArray;

           valueJToken: JsonToken;

           json_Token6: JsonToken;

           json_Array9: JsonArray;

           r: Integer;

           Z: Integer;

           json_Array7: JsonArray;

           json_Array1: JsonArray;

           json_Token9: JsonToken;

           json_Object7: JsonObject;

           json_Object10: JsonObject;

           json_Array2: JsonArray;

           json_Token7: JsonToken;

           InvoiceNo: Text[50];

           i: Integer;

           j: Integer;

           k: integer;

           O: Integer;

           m: integer;

           p: integer;

           skudetails1: Record skudetails;

           skuList: Text;

       begin

           // Add the payload to the content

           content.WriteFrom(payload);

           Json_object.WriteTo(tmpString);

           content.WriteFrom(tmpString);

           content.ReadAs(tmpString);

           // Retrieve the contentHeaders associated with the content

           content.GetHeaders(contentHeaders);

           contentHeaders.Remove('Content-Type');

           contentHeaders.Add('Content-Type', 'application/x-www-form-urlencoded');

           contentHeaders.Remove('ApiKey');

           client.DefaultRequestHeaders.Add('ApiOwner', 'xxx');

           client.DefaultRequestHeaders.Add('ApiKey', 'xxx');

           // contentHeaders.Add('accept', 'application/json');

           content.GetHeaders(contentHeaders);

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

           Message(responseText);

           Clear(skuList);

           if json_Token.ReadFrom(responseText) then begin

               if json_Token.IsObject then begin

                   json_Object1 := json_Token.AsObject();

                   if json_Object1.Get('skulist', valueJToken) then begin

                       if valueJToken.IsArray then begin

                           json_Array := valueJToken.AsArray();

                           for i := 0 to json_Array.count - 1 do begin

                               json_Array.Get(i, json_Token);

                               if json_Token.IsObject then begin

                                   json_Object := json_Token.AsObject();

                                   //To get skuList_skucode

                                   if json_Object.Get('skuList_skucode', valueJToken) then begin

                                       if valueJToken.IsValue then begin

                                           if not valueJToken.AsValue().IsNull then begin

                                               skudetails.Init();

                                               skudetails.skuList_skucode := valueJToken.AsValue().AsText();

                                           end;

                                       end;

                                   end;

                                   //to get skuList_skuName

                                   if json_Object.Get('skuList_skuName', valueJToken) then begin

                                       if valueJToken.IsValue then begin

                                           if not valueJToken.AsValue().IsNull then begin

                                               skudetails.skuList_skuName := valueJToken.AsValue().AsText();

                                               // skuList := valueJToken.AsValue().AsText();

                                           end;

                                       end;

                                   end;

                                   //--

                                   //To get skuList_classification

                                   if json_Object.Get('skuList_classification', valueJToken) then begin

                                       if valueJToken.IsValue then begin

                                           if not valueJToken.AsValue().IsNull then begin

                                               skudetails.skuList_classification := valueJToken.AsValue().AsText();

                                           end;

                                       end;

                                   end;

                                   //To get skuList_parentSKU

                                   if json_Object.Get('skuList_parentSKU', valueJToken) then begin

                                       if valueJToken.IsValue then begin

                                           if not valueJToken.AsValue().IsNull then begin

                                               skudetails.skuList_parentSKU := valueJToken.AsValue().AsText();

                                           end;

                                       end;

                                   end;

                                   //To get skuList_desc

                                   if json_Object.Get('skuList_desc', valueJToken) then begin

                                       if valueJToken.IsValue then begin

                                           if not valueJToken.AsValue().IsNull then begin

                                               skudetails.skuList_desc := valueJToken.AsValue().AsText();

                                           end;

                                       end;

                                   end;

                                   //To get "skuList_detailDESC

                                   if json_Object.Get('"skuList_detailDESC', valueJToken) then begin

                                       if valueJToken.IsValue then begin

                                           if not valueJToken.AsValue().IsNull then begin

                                               skudetails.skuList_detailDESC := valueJToken.AsValue().AsText();

                                           end;

                                       end;

                                   end;

                                   //To get skuList_length

                                   if json_Object.Get('skuList_length', valueJToken) then begin

                                       if valueJToken.IsValue then begin

                                           if not valueJToken.AsValue().IsNull then begin

                                               skudetails.skuList_length := valueJToken.AsValue().AsText();

                                           end;

                                       end;

                                   end;

                                   //To get skuList_width

                                   if json_Object.Get('skuList_width', valueJToken) then begin

                                       if valueJToken.IsValue then begin

                                           if not valueJToken.AsValue().IsNull then begin

                                               skudetails.skuList_width := valueJToken.AsValue().AsText();

                                           end;

                                       end;

                                   end;

                                   //To get skuList_weight

                                   if json_Object.Get('skuList_weight', valueJToken) then begin

                                       if valueJToken.IsValue then begin

                                           if not valueJToken.AsValue().IsNull then begin

                                               skudetails.skuList_weight := valueJToken.AsValue().AsText();

                                           end;

                                       end;

                                   end;

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,238

#2
YUN ZHU Profile Picture

YUN ZHU 773 Super User 2025 Season 2

#3
Sumit Singh Profile Picture

Sumit Singh 630

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans