Options

Accessing External API via Rest Json using Post method

ebitasebitas Member Posts: 71
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 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
Sign In or Register to comment.