Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics NAV (Archived)

How to call REST api from NAV 2013

Posted on by 35

Let's understand the scenario, 

I have a xmlport and I have to use that in a code unit to call REST api and need to submit xml data as body content of the api and it will have few headers too.

But I am unable to post data in content as there is no Codeunit 1297 Http Web Request Mgt. in NAV 2013.

Unable to perform operations on Stream writer.

NAV is completely new for me, I am trying to learn it and improving myself day by day. But still thrs a lot to learn.

Is there any one who can help me with that? 

(Please suggest me a better resource to learn NAV too if possible.)

Thank you !

*This post is locked for comments

  • mbambia Profile Picture
    mbambia 15 on at
    RE: How to call REST api from NAV 2013

     I tried to implement your code but I have an error when I try to compile the Codeunit :

    You have specified an unknown variable. "WebRequest"

    Define the variable under 'Global C/AL symbols'.

    What is WebRequest?

  • Suggested answer
    Krish Roy Profile Picture
    Krish Roy 35 on at
    RE: How to call REST api from NAV 2013

    VAR

         HttpWebRequest@1000000001 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebRequest" RUNONCLIENT;

         HttpWebResponse@1000000004 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebResponse" RUNONCLIENT;

         HttpStatusCode@1000000005 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpStatusCode" RUNONCLIENT;

         Stream@1000000014 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.Stream" RUNONCLIENT;

         TextEncoding@1000000004 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Text.Encoding" RUNONCLIENT;

         ResStreamReader@1000000010 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.StreamReader" RUNONCLIENT;

         ResStream@1000000012 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.Stream" RUNONCLIENT;

         Response@1000000015 : Text;

         RequestData@1000000006 : Text;

         HttpWebRequest:=HttpWebRequest.HttpWebRequest;

         HttpWebRequest:=WebRequest.Create('http://API_URL');

         HttpWebRequest.ContentType:='application/xml';

         HttpWebRequest.Method:='POST';

         Stream:=HttpWebRequest.GetRequestStream();

         Stream.Write(TextEncoding.ASCII.GetBytes(RequestData),0,TextEncoding.ASCII.GetBytes(RequestData).Length);

         Stream.Close();

         HttpWebResponse:=HttpWebResponse.HttpWebResponse;

         HttpWebResponse:=HttpWebRequest.GetResponse();

         HttpResStatus:=HttpWebResponse.StatusCode();

     CLEAR(Response);

         IF HttpResStatus=200 THEN BEGIN

           ResStream:=HttpWebResponse.GetResponseStream();

           ResStreamReader:=ResStreamReader.StreamReader(ResStream,TextEncoding.Unicode);

           Response:=ResStreamReader.ReadToEnd();

         END ELSE BEGIN

           ResStream:=HttpWebResponse.GetResponseStream();

           ResStreamReader:=ResStreamReader.StreamReader(ResStream,TextEncoding.Unicode);

           Response:=HttpWebResponse.StatusDescription;

         END;

  • Krish Roy Profile Picture
    Krish Roy 35 on at
    RE: How to call REST api from NAV 2013

    VAR

         HttpWebRequest@1000000001 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebRequest" RUNONCLIENT;

         HttpWebResponse@1000000004 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebResponse" RUNONCLIENT;

         HttpStatusCode@1000000005 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpStatusCode" RUNONCLIENT;

         Stream@1000000014 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.Stream" RUNONCLIENT;

         TextEncoding@1000000004 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Text.Encoding" RUNONCLIENT;

         ResStreamReader@1000000010 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.StreamReader" RUNONCLIENT;

         ResStream@1000000012 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.Stream" RUNONCLIENT;

         Response@1000000015 : Text;

         RequestData@1000000006 : Text;

         HttpWebRequest:=HttpWebRequest.HttpWebRequest;

         HttpWebRequest:=WebRequest.Create('http://API_URL');

         HttpWebRequest.ContentType:='application/xml';

         HttpWebRequest.Method:='POST';

         Stream:=HttpWebRequest.GetRequestStream();

         Stream.Write(TextEncoding.ASCII.GetBytes(RequestData),0,TextEncoding.ASCII.GetBytes(RequestData).Length);

         Stream.Close();

         HttpWebResponse:=HttpWebResponse.HttpWebResponse;

         HttpWebResponse:=HttpWebRequest.GetResponse();

         HttpResStatus:=HttpWebResponse.StatusCode();

     CLEAR(Response);

         IF HttpResStatus=200 THEN BEGIN

           ResStream:=HttpWebResponse.GetResponseStream();

           ResStreamReader:=ResStreamReader.StreamReader(ResStream,TextEncoding.Unicode);

           Response:=ResStreamReader.ReadToEnd();

         END ELSE BEGIN

           ResStream:=HttpWebResponse.GetResponseStream();

           ResStreamReader:=ResStreamReader.StreamReader(ResStream,TextEncoding.Unicode);

           Response:=HttpWebResponse.StatusDescription;

         END;

  • Krish Roy Profile Picture
    Krish Roy 35 on at
    RE: How to call REST api from NAV 2013

    Krishna, 22:15

    VAR

         HttpWebRequest@1000000001 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebRequest" RUNONCLIENT;

         HttpWebResponse@1000000004 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebResponse" RUNONCLIENT;

         HttpStatusCode@1000000005 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpStatusCode" RUNONCLIENT;

         Stream@1000000014 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.Stream" RUNONCLIENT;

         TextEncoding@1000000004 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Text.Encoding" RUNONCLIENT;

         ResStreamReader@1000000010 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.StreamReader" RUNONCLIENT;

         ResStream@1000000012 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.Stream" RUNONCLIENT;

         Response@1000000015 : Text;

         RequestData@1000000006 : Text;

         HttpWebRequest:=HttpWebRequest.HttpWebRequest;

         HttpWebRequest:=WebRequest.Create('http://API_URL');

         HttpWebRequest.ContentType:='application/xml';

         HttpWebRequest.Method:='POST';

         Stream:=HttpWebRequest.GetRequestStream();

         Stream.Write(TextEncoding.ASCII.GetBytes(RequestData),0,TextEncoding.ASCII.GetBytes(RequestData).Length);

         Stream.Close();

         HttpWebResponse:=HttpWebResponse.HttpWebResponse;

         HttpWebResponse:=HttpWebRequest.GetResponse();

         HttpResStatus:=HttpWebResponse.StatusCode();

     CLEAR(Response);

         IF HttpResStatus=200 THEN BEGIN

           ResStream:=HttpWebResponse.GetResponseStream();

           ResStreamReader:=ResStreamReader.StreamReader(ResStream,TextEncoding.Unicode);

           Response:=ResStreamReader.ReadToEnd();

         END ELSE BEGIN

           ResStream:=HttpWebResponse.GetResponseStream();

           ResStreamReader:=ResStreamReader.StreamReader(ResStream,TextEncoding.Unicode);

           Response:=HttpWebResponse.StatusDescription;

         END;

  • Krish Roy Profile Picture
    Krish Roy 35 on at
    RE: How to call REST api from NAV 2013

    U7Krishna, 22:15 VAR HttpWebRequest@1000000001 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebRequest" RUNONCLIENT; HttpWebResponse@1000000004 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebResponse" RUNONCLIENT; HttpStatusCode@1000000005 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpStatusCode" RUNONCLIENT; Stream@1000000014 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.Stream" RUNONCLIENT; TextEncoding@1000000004 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Text.Encoding" RUNONCLIENT; ResStreamReader@1000000010 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.StreamReader" RUNONCLIENT; ResStream@1000000012 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.Stream" RUNONCLIENT; Response@1000000015 : Text; RequestData@1000000006 : Text; HttpWebRequest:=HttpWebRequest.HttpWebRequest; HttpWebRequest:=WebRequest.Create('http://API_URL'); HttpWebRequest.ContentType:='application/xml'; HttpWebRequest.Method:='POST'; Stream:=HttpWebRequest.GetRequestStream(); Stream.Write(TextEncoding.ASCII.GetBytes(RequestData),0,TextEncoding.ASCII.GetBytes(RequestData).Length); Stream.Close(); HttpWebResponse:=HttpWebResponse.HttpWebResponse; HttpWebResponse:=HttpWebRequest.GetResponse(); HttpResStatus:=HttpWebResponse.StatusCode(); CLEAR(Response); IF HttpResStatus=200 THEN BEGIN ResStream:=HttpWebResponse.GetResponseStream(); ResStreamReader:=ResStreamReader.StreamReader(ResStream,TextEncoding.Unicode); Response:=ResStreamReader.ReadToEnd(); END ELSE BEGIN ResStream:=HttpWebResponse.GetResponseStream(); ResStreamReader:=ResStreamReader.StreamReader(ResStream,TextEncoding.Unicode); Response:=HttpWebResponse.StatusDescription; END;

  • Krish Roy Profile Picture
    Krish Roy 35 on at
    RE: How to call REST api from NAV 2013

    Hi,

    In Navision 2013 there is no unbuilt codeunit to call API (http web management) you have to use dotnet variables and write it in the same way in Navision.

    Here is the code.

    VAR
    HttpWebRequest@1000000001 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebRequest" RUNONCLIENT;
    HttpWebResponse@1000000004 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebResponse" RUNONCLIENT;
    HttpStatusCode@1000000005 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpStatusCode" RUNONCLIENT;
    Stream@1000000014 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.Stream" RUNONCLIENT;
    TextEncoding@1000000004 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Text.Encoding" RUNONCLIENT;
    ResStreamReader@1000000010 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.StreamReader" RUNONCLIENT;
    ResStream@1000000012 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.Stream" RUNONCLIENT;
    Response@1000000015 : Text;
    RequestData@1000000006 : Text;

    HttpWebRequest:=HttpWebRequest.HttpWebRequest;
    HttpWebRequest:=WebRequest.Create('http://API_URL');
    HttpWebRequest.ContentType:='application/xml';
    HttpWebRequest.Method:='POST';
    Stream:=HttpWebRequest.GetRequestStream();
    Stream.Write(TextEncoding.ASCII.GetBytes(RequestData),0,TextEncoding.ASCII.GetBytes(RequestData).Length);
    Stream.Close();

    HttpWebResponse:=HttpWebResponse.HttpWebResponse;
    HttpWebResponse:=HttpWebRequest.GetResponse();
    HttpResStatus:=HttpWebResponse.StatusCode();
    CLEAR(Response);
    IF HttpResStatus=200 THEN BEGIN
    ResStream:=HttpWebResponse.GetResponseStream();
    ResStreamReader:=ResStreamReader.StreamReader(ResStream,TextEncoding.Unicode);
    Response:=ResStreamReader.ReadToEnd();
    END ELSE BEGIN
    ResStream:=HttpWebResponse.GetResponseStream();
    ResStreamReader:=ResStreamReader.StreamReader(ResStream,TextEncoding.Unicode);
    Response:=HttpWebResponse.StatusDescription;
    END;

  • RE: How to call REST api from NAV 2013

    Hi Krish,

    I have the same problem that You solved, please can You explain the way You followed to reach your goal?

    Thank You in advance

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to call REST api from NAV 2013

    Would be nice if you share it with us here for the next person..

  • Krish Roy Profile Picture
    Krish Roy 35 on at
    RE: How to call REST api from NAV 2013

    "Failure is the best Teacher "

    Finally I have created a Code unit to call API from NAV without using Dot net dll.

  • Krish Roy Profile Picture
    Krish Roy 35 on at
    RE: How to call REST api from NAV 2013

    Thank you EveryOne.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans