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';









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

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


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




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.



  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

      IF HttpWebRequestMgt.GetResponse(Instr,HttpStatusCode,ResponseHeaders) THEN BEGIN
      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… 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

  4. Thanks for this post, i was trying to create a variable for Json Object. i got this error

    Could not load type Newtonsoft.Json.Linq.Jobject.’Newtonsoft.Json, Version=, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed’.

    What can i do?


      1. Hi Donah,
        Nav 2017 already has the .dll included in the program files location. But downloading ensures you have the latest version.

  5. Hi Jaspreet,

    These are very excellent articles and it helps alot, but I am stuck at a point where I am trying to call a URL which uses SSO(single sign on) and there it asks for credentials, even though I have supplied the correct credentials to the HttpWebRequest.Credential property still getting issue of 401 un authorized error.

    Can you please help in my case.


  6. Hi,
    I have a problem running CU 1297 on NAS server. My integration works perfectly fine on the client version (no errors, no dialog windows, nothing), but whenever I run it on NAS it complains about client callbacks for all dotnet variables CU 1297 is using. All of them are already set up not to run on client (as suggested in some forums) but that is not solving the problem. Is it possible to run CU1297 on NAS as all, and if not, is there any alternative to it which would work on NAS?

Leave a Reply

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