Accessing External API via Rest Json using Post method

ebitasebitas Posts: 68Member
edited 2018-09-21 in NAV Three Tier
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 [email protected]([email protected] : Text;[email protected] : 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 [email protected]([email protected] : Text;[email protected] : 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 [email protected](VAR [email protected] : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebRequest";[email protected] : Text;[email protected] : 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 [email protected](VAR [email protected] : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebRequest";VAR [email protected] : DotNet "'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.HttpWebResponse";[email protected] : 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
Sign In or Register to comment.