Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics NAV (Archived)

Accessing External API via Rest Json using Post method

(0) ShareShare
ReportReport
Posted on by 70

Hi;

I created the attached codeunit to communicate with External API via Rest method. How ever I'm getting an error upon run saying "The Remote Server returned an error: (504) Gateway timeout". I'm guessing it is because of the Authentication (combination of Authorization and Key). Here are the Requirements and what I did: (Please note that key I'm providing below is just a sample and not the actual (For security) ) 

The authorization is too long so I just typed some of it here :

xa3js8hmu3.execute-api.us-east-1.amazonaws.com/.../parts

HEADERS

Content-Type: application/json
x-api-key: LViHg8iYYa6VPZqhyemO92swvuo8RiZbage4Y4en

Authorization:
Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6IlJqaEZNalpDTkRkRk1UY3dRamMwTURBeE5EZ3pOelkxT0RjMU5ERk

the section of the code in NAV that handle this:

    PROCEDURE UploadJson@1000000012(WebServiceURL@1000000000 : Text;String@1000000001 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.String");
    VAR
      HttpWebRequest@1000000002 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebRequest";
      HttpWebResponse@1000000003 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebResponse";
    BEGIN
      CreateWebRequest(HttpWebRequest,WebServiceURL,'POST');
      SetRequestStream(HttpWebRequest,String);
      DoWebRequest(HttpWebRequest,HttpWebResponse,'');
      GetResponseStream(HttpWebResponse,String);
    END;

    PROCEDURE CreateWebRequest@1000000015(VAR HttpWebRequest@1000000002 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebRequest";WebServiceURL@1000000001 : Text;Method@1000000000 : Text);
    BEGIN
      HttpWebRequest := HttpWebRequest.Create(WebServiceURL);
      HttpWebRequest.Timeout := 300000;
      HttpWebRequest.Method := Method;
      HttpWebRequest.Accept := 'application/json';
      HttpWebRequest.ContentType := 'application/json';

      HttpWebRequest.Headers.Add('Authorization', dhbAuthText[1]+dhbAuthText[2]+dhbAuthText[3]+dhbAuthText[4]+dhbAuthText[5]+dhbAuthText[6]);
      HttpWebRequest.Headers.Add('x-api-key','LViHg8iYYa6VPZqhyemO92swvuo8RiZbage4Y4en');
    END;

As I mentioned I did attach the whole codeunit as a text file.
What am I doing wrong or missing here? Please help..  Thank you

 

CodeUnitObject.txt

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Accessing External API via Rest Json using Post method

    Hi Ebitas,

    Can you please the Fob through the mail gmpradikrish@gmail.com.? Thanks in Advance

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Accessing External API via Rest Json using Post method

    Hi can u guide me how can i update my external API using these codeunits that you have mentioned. Thanks in advance.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Accessing External API via Rest Json using Post method

    Thank you so much

  • Dietmar Wehinger Profile Picture
    Dietmar Wehinger 65 on at
    RE: Accessing External API via Rest Json using Post method

    It should work for all versions but in in 2018 you already have codeunits that deal with Jason and API functionality so You can use them instead. Check out the following codeunits #1237, 1238, 1297 and 1299

    hope that helps

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Accessing External API via Rest Json using Post method

    Thank you so much for the answer. I am using nav 2018 , will this will be useful in that version too?

    My mailID: gmpradeepkrishna@gmail.co.

    Thanks again.

  • Dietmar Wehinger Profile Picture
    Dietmar Wehinger 65 on at
    RE: Accessing External API via Rest Json using Post method

    hi;

    Sure I can provide you with some help. The main thing is to install Newtonsoft  on your server or PC. Then assign the correct DoNet in NAV

    StringBuilder DotNet System.Text.StringBuilder.'mscorlib'
    StringWriter DotNet System.IO.StringWriter.'mscorlib'
    StringReader DotNet System.IO.StringReader.'mscorlib'
    JsonTextWriter DotNet Newtonsoft.Json.JsonTextWriter.'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'
    JsonTextReader DotNet Newtonsoft.Json.JsonTextReader.'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'
    HttpStringContent DotNet System.Net.Http.StringContent.'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
    Json DotNet System.String.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    JsonConvert DotNet Newtonsoft.Json.JsonConvert.'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'
    JsonArray DotNet Newtonsoft.Json.Linq.JArray.'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'
    StringContent DotNet System.Net.Http.StringContent.'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
    Encoding DotNet System.Text.Encoding.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    Also; I assume you'll need communicate and get an Authentication Token first then you'll have access:


    // Start - Get Authorization code - *******************
    StartJSon;
    EndJSon;
    UserName := 'EnterUsernamehere';
    Password := 'EnterPassword here';
    Json := Json.Copy(GetJSon);
    dhbCnt := 1;
    dhbAuthText := '';
    UploadJSon('vceni4hd08.execute-api.us-west-3.amazonaws.com/.../auth',Json,'POST');
    // End - Get Authorization code - *******************

    Also in the codeunit you'll need to create a function the you can send parameters to it the code is below:


    HttpClient := HttpClient.HttpClient();
    HttpClient.DefaultRequestHeaders.Add('x-api-key','Enter the API key for the customer here');
    HttpClient.DefaultRequestHeaders.Add('Authorization',dhbAuthText);
    HttpClient.DefaultRequestHeaders.Add('ContentType','application/json');
    HttpClient.BaseAddress := Uri.Uri(BaseURL);
    CASE RestMethod OF
    'GET':
    HttpResponseMessage := HttpClient.GetAsync(Method).Result;
    'POST':
    HttpResponseMessage := HttpClient.PostAsync(Method,HttpContent).Result;
    'PUT':
    HttpResponseMessage := HttpClient.PutAsync(Method,HttpContent).Result;
    'DELETE':
    HttpResponseMessage := HttpClient.DeleteAsync(Method).Result;
    END;

    HttpResponseMessage.EnsureSuccessStatusCode();
    String := HttpResponseMessage.Content.ReadAsStringAsync.Result;

    the local Parameters and  variables for the above function are

    Var Name DataType Subtype
    No BaseURL Text
    No Method Text
    No RestMethod Text
    Yes HttpContent DotNet System.Net.Http.HttpContent.'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
    Yes HttpResponseMessage DotNet System.Net.Http.HttpResponseMessage.'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
    Name DataType Subtype
    HttpClient DotNet System.Net.Http.HttpClient.'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
    Uri DotNet System.Uri.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    StreamReader DotNet System.IO.StreamReader.'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    MemoryStream DotNet System.IO.MemoryStream.'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    String DotNet System.String.'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    TempPostingExchField Record Posting Exch. Field (Material)
    dhbFile File  

    Send me your email and i see if I can send you the codeunit , but i'll need to modify it a little to remove any customer's IDs and passwords first.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Accessing External API via Rest Json using Post method

    Hi , I need to do the same. Can u say what have u done?. Please can u guide me.

  • Dietmar Wehinger Profile Picture
    Dietmar Wehinger 65 on at
    RE: Accessing External API via Rest Json using Post method

    Is there a documentation describes how to Integrate NAV and Postman?

  • Dietmar Wehinger Profile Picture
    Dietmar Wehinger 65 on at
    RE: Accessing External API via Rest Json using Post method

    Thanks Manish.. The purpose of the project is to send and receive data from and to NAV  multiple times a day That's why I want the code to be in NAV.. Will Postman allow me to do that?

  • ManishS Profile Picture
    ManishS 6,578 on at
    RE: Accessing External API via Rest Json using Post method

    Have you tried it in Postman by google. Check in here first.

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 Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,492 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans