consume an external web service from navision

Hello,

I would like to consume an external web service from navision, my current version is BC 14 and i try to do it using codeunit.

thank you in advance.

Answers

  • maxklein
    maxklein Member Posts: 8
    its simple ;)

    OBJECT Codeunit 50100 ConsumeWebService
    {
    OBJECT-PROPERTIES
    {
    OnRun=BEGIN
    CallExternalAPI();
    END;
    }
    PROPERTIES
    {
    SingleInstance=Yes;
    }
    LOCAL PROCEDURE CallExternalAPI@1();
    VAR
    HttpWebRequest@1000 : DotNet "System.Net.HttpWebRequest";
    HttpWebResponse@1001 : DotNet "System.Net.HttpWebResponse";
    StreamReader@1002 : DotNet "System.IO.StreamReader";
    RequestStream@1003 : DotNet "System.IO.Stream";
    StreamWriter@1004 : DotNet "System.IO.StreamWriter";
    Encoding@1005 : DotNet "System.Text.Encoding";
    Uri@1006 : DotNet "System.Uri";
    JsonText@1007 : Text[1024];
    ResponseText@1008 : Text;
    ResponseStream@1009 : DotNet "System.IO.Stream";
    BEGIN
    JsonText := '{ "exampleKey": "exampleValue" }'; // Replace with your JSON payload

    Uri := Uri.Uri('https://your-api-url.com/endpoint'); // Replace with your URL
    HttpWebRequest := HttpWebRequest.Create(Uri);
    HttpWebRequest.Method := 'POST';
    HttpWebRequest.ContentType := 'application/json';

    Encoding := Encoding.UTF8;
    RequestStream := HttpWebRequest.GetRequestStream();
    StreamWriter := StreamWriter.StreamWriter(RequestStream, Encoding);
    StreamWriter.Write(JsonText);
    StreamWriter.Close();

    HttpWebResponse := HttpWebRequest.GetResponse();
    ResponseStream := HttpWebResponse.GetResponseStream();
    StreamReader := StreamReader.StreamReader(ResponseStream);
    ResponseText := StreamReader.ReadToEnd();

    MESSAGE('Response: %1', ResponseText);
    END;
    }
    Developer by passion
  • RockWithNAV
    RockWithNAV Member Posts: 1,193
    Too many ways.

    Simple Get Method

    HttpWebRequest := HttpWebRequest.HttpWebRequest;
    Uri := Uri.Uri('https://api.example.com/data');

    HttpWebRequest := HttpWebRequest.Create(Uri);
    HttpWebRequest.Method := 'GET';
    HttpWebRequest.ContentType := 'application/json';

    HttpWebResponse := HttpWebRequest.GetResponse();
    StreamReader := StreamReader.StreamReader(HttpWebResponse.GetResponseStream());
    ResponseText := StreamReader.ReadToEnd();

    Message(ResponseText);

    Variables you can use as mentioned as above :smiley:
  • Miklos_Hollender
    Miklos_Hollender Member Posts: 1,628

    I would not do so. As the above examples show, it is very tedious, what you get is texts to parse manually, you do not have an object proxy.

    It is better to use an external program, for example in PowerShell Export-ODataProxy, read the data in PS from the web service, and push the data into the BC web service.

  • RockWithNAV
    RockWithNAV Member Posts: 1,193

    @Miklos_Hollender - This one sounds enticing, do you have a sample solution or the codebase for this?

    Within NAV interface never had the experience to navigate via this route.

  • Miklos_Hollender
    Miklos_Hollender Member Posts: 1,628

    not at the moment, but just ask claude.ai (no registration needed)