Dynamics 365 Dynamics NAV 2017

Post to a rest api in Dynamics NAV

Post is usually used when we want to submit some parameter to the server.

The paramaters unlike rest are not put in the link itself but are sent in ‘Body’.

In Nav , We can post using either Codeunit 1297 Http Web Request Mgt. or using System.HttpClient Control add-in among other methods.

Let’s go over the syntax when using CU 1297. I will be posting to a test api hosted by https://reqres.in/ for demo. Documentation for the api can be found there. We will be using api/users for the demo

First create a new codeunit in your range.

Syntax in Nav

 

PostUrl := 'https://reqres.in/api/users';

HttpWebRequestMgt.Initialize(PostUrl);

HttpWebRequestMgt.DisableUI;

HttpWebRequestMgt.SetMethod('POST');

HttpWebRequestMgt.SetContentType('application/json');

HttpWebRequestMgt.SetReturnType('application/json');

HttpWebRequestMgt.AddBodyAsText(body);


TempBlob.INIT;

TempBlob.Blob.CREATEINSTREAM(Instr);

IF HttpWebRequestMgt.GetResponse(Instr,HttpStatusCode,ResponseHeaders) THEN BEGIN

 MESSAGE('httpstatuscode : '+HttpStatusCode.ToString);

 MESSAGE(ResponseHeaders.ToString);

 ApiResult := TempBlob.ReadAsText('',TEXTENCODING::UTF8);

 MESSAGE(ApiResult);

 JObject:=JObject.Parse(ApiResult);

 MESSAGE(JObject.GetValue('job').ToString);

END ELSE MESSAGE('2'+RegistrationError);



You will need to define these variables.

You need text constants as well. Note that body can be a variable too . The codeunit can also accept a blob type in case your data is large. Just use HttpWebRequestMgt.AddBodyBlob(nameofblob); instead of HttpWebRequestMgt.AddBodyAsText(body);

 

 

The output from api is stored in tempblob which can then be read in a text string.

Refer to  https://ctrlshiftf4.com/2017/05/17/consume-openweather-rest-api-in-dynamics-nav/ as i have explained how to parse data from Rest Api there in detail.

 

8 comments

  1. Very Nice 🙂 I have a problem.
    I get around 80K chars in return from the web service POST (which is by design).
    It is returning af Base64 text which results in a .pdf.
    What will I have to do in order to store or temporarily store this text so i can convert it else where?
    Can I alter the ApiResult := tempBlob.ReadAsText or what to do?

    1. You can try to store it in a bigtext. And then use the dotnet library to convert base64 to PDF.

      Other thing you can try simply return the PDF in the api. Nav will recieve it in the blob and you can export it to the location you want.
      I have done this with Images. Might also work for PDF.
      Will try and let you know 🙂

      EDIT: It works. Here is the code

      Url:=’http://kmmc.in/wp-content/uploads/2014/01/lesson2.pdf’;
      HttpWebRequestMgt.Initialize(Url);
      HttpWebRequestMgt.DisableUI;
      HttpWebRequestMgt.SetMethod(‘GET’);
      TempBlob.INIT;
      TempBlob.Blob.CREATEINSTREAM(Instr);
      IF HttpWebRequestMgt.GetResponse(Instr,HttpStatusCode,ResponseHeaders) THEN BEGIN
      TempBlob.Blob.EXPORT(‘D:\abc.pdf’);
      END ELSE MESSAGE(Error);

      1. Very cool 😛 but you see. I get a 8000 char string(text) back not the .pdf
        GET is not used, at i need to POST to get the 8000 char text.
        I can change to Bigtext if i can put body into variables instead og text constant.
        How would i go that? ultimatly i need to that this, so i can bind the json i POST to the fields in the sales order fields.
        Makes sense :P?
        The posted codes does look gr8 thou.

        1. If you are talking about the response then you already get it in BLOB so you are fine using READASTEXT.

          If you are talking about the body that you will be posting to the api and want to post more than just text, You can.
          In the HttpWebRequest codeunit there are other AddBody functions where you can even post a BLOB to the api.

      2. Thx for the fast reply.
        Let me try to explain. I think maybe i don’t understand the code.
        I POST body.
        This gives me an answer with a 8000 lenght text.
        Where does the code fail? I know it’s because it’s to big, and BIGTEXT would fix this.
        But what do i change to make sure that i’m able to get the response into a BIGTEXT object or storage instead of text (which is to small to handle 8k)?

  2. Well okay.. I’ve changes nothing and now it goes through.
    I do get an error on the Newtonesoft.json.dll.
    And I’ve tried both a .Net 4.5 and 4.0 version.
    It says: Could Not Load an instance of the following .NET Framework object: Newtonesoft.jon.dll version 10.0.0.0… and so on.

  3. Okay, Update! I’ve downgradet newtonsoft to 6.0.1 and now i get a new error:
    A DotNet variable has not been instantiated. Attempting to call Tostring in CodeUnit (mycodeunitname): InitializeHttpPost.
    I know what it means.. but why? 😀
    I’m assuming that it’s the MESSAGE(JObject.GetValue(‘job’).ToString).
    I’ve change ‘job’ to ‘pdf’ since that the json property name i want the value of.

    I’m sry for the frequest questions and i thank you for your time.
    Please feel free to remove all my question so your blog dosn’t look all messy 🙂

    1. Can you mail me the txt file of your code (find it in my Contact me page).
      I will have a look and see if I can find something

      Version should not matter

Leave a Reply

Your email address will not be published. Required fields are marked *