Accessing External API via Rest Json using Post method

ebitas
Member Posts: 71
Hi;
I created a 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 :
https://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");
BEGIN
CreateWebRequest(HttpWebRequest,WebServiceURL,'POST');
SetRequestStream(HttpWebRequest,String);
DoWebRequest(HttpWebRequest,HttpWebResponse,'');
GetResponseStream(HttpWebResponse,String);
END;
PROCEDURE UploadJson@1000000012(WebServiceURL@1000000000 : Text;String@1000000001 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.String");
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;
The error occurs in this section:
PROCEDURE DoWebRequest@1000000009(VAR HttpWebRequest@1000000000 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebRequest";VAR HTTPWebResponse@1000000001 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebResponse";IgnoreCode@1000000002 : Code[10]);
BEGIN
NAVWebRequest := NAVWebRequest.NAVWebRequest;
IF NOT NAVWebRequest.doRequest(HttpWebRequest,HttpWebException,HTTPWebResponse) THEN
IF (IgnoreCode = '') OR (STRPOS(HttpWebException.Message,IgnoreCode) = 0) THEN
ERROR(HttpWebRequestError,HttpWebException.Status.ToString,HttpWebException.Message);
END;
What am I doing wrong or missing here? Please help.. Thank you
I created a 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 :
https://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");
BEGIN
CreateWebRequest(HttpWebRequest,WebServiceURL,'POST');
SetRequestStream(HttpWebRequest,String);
DoWebRequest(HttpWebRequest,HttpWebResponse,'');
GetResponseStream(HttpWebResponse,String);
END;
PROCEDURE UploadJson@1000000012(WebServiceURL@1000000000 : Text;String@1000000001 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.String");
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;
The error occurs in this section:
PROCEDURE DoWebRequest@1000000009(VAR HttpWebRequest@1000000000 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebRequest";VAR HTTPWebResponse@1000000001 : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebResponse";IgnoreCode@1000000002 : Code[10]);
BEGIN
NAVWebRequest := NAVWebRequest.NAVWebRequest;
IF NOT NAVWebRequest.doRequest(HttpWebRequest,HttpWebException,HTTPWebResponse) THEN
IF (IgnoreCode = '') OR (STRPOS(HttpWebException.Message,IgnoreCode) = 0) THEN
ERROR(HttpWebRequestError,HttpWebException.Status.ToString,HttpWebException.Message);
END;
What am I doing wrong or missing here? Please help.. Thank you
0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions