Page 1 of 1

External REST API call through javascript

Posted: Fri Sep 21, 2018 3:16 am
by dinusha51025
Hi team

i need to call the external application REST API through the using token.
in my process marker application, i need to get it done through the java-script (by using button click). Because i want to show the return parameters form API on Dynaform fields.

please can some one help me . if you can attach the java-script code , that is good

i used following code but not working
Code: Select all
function getDetails() {
   var req = new XMLHttpRequest();
   var sUrl ="api http link with parameeters";
   var sTkn ="getTokenExtenalApp";  
  
   req.open("GET", sUrl , true);
   req.setRequestHeader("Authorization", "Bearer " + sTkn); 
   
   alert("Error status code: " + req.setRequestHeader);
  
   req.onreadystatechange = function() {
     alert ("working");
     if (req.readyState==4) {
        if (req.status==200) {        
           var aShowRetuen = JSON.parse(req.responseText);
           alert(aShowRetuen);       
        }
        else {
           alert("Error status code: " + req.status);
        }
  // }
   req.send(null);
  }
}

Re: External REST API call through javascript

Posted: Fri Sep 21, 2018 9:46 pm
by amosbatto
You need to uncomment your \\ } because otherwise you don't have matching curly braces.
Your req.send() needs to be outside the req.onreadystatechange event handler.
You need to set a valid URL and access token in your sUrl and sTkn variables.

Try it this way:
Code: Select all
function getDetails() {
   var req = new XMLHttpRequest();
   var sUrl ="api http link with parameeters";
   var sTkn ="getTokenExtenalApp";  
  
   req.open("GET", sUrl , true);
   req.setRequestHeader("Authorization", "Bearer " + sTkn); 
   
   alert("Error status code: " + req.setRequestHeader);
  
   req.onreadystatechange = function() {
     alert ("working");
     if (req.readyState==4) {
        if (req.status==200) {        
           var aShowRetuen = JSON.parse(req.responseText);
           alert(aShowRetuen);       
        }
        else {
           alert("Error status code: " + req.status);
        }
     }    
   }
   
   req.send(null);
}

Re: External REST API call through javascript

Posted: Fri Sep 21, 2018 9:49 pm
by amosbatto
By the way, it is much easier to use $.ajax().
See these examples:
https://wiki.processmaker.com/3.1/JavaS ... ing_jQuery

Re: External REST API call through javascript

Posted: Tue Sep 25, 2018 6:12 am
by dinusha51025
Hi amosbatto

Thanks for your reply with java script code....
i will try and let you know the status!!

thx

Re: External REST API call through javascript

Posted: Tue Sep 25, 2018 8:08 am
by dinusha51025
Dear amosbatto and team

i tried to fix this issue number of time by changing java-script code and the REST API link and token.
but every time the req.status is give '0' to out

(please note : the relevant rest API's are executed without any errors through 'POSTMAN' correctly)

our REST api links are executed as HTTPS links. please let me know whether 'var req = new XMLHttpRequest();' is working for https links?

thx

Re: External REST API call through javascript

Posted: Tue Sep 25, 2018 8:03 pm
by amosbatto
Are you using the complete URL with "https"?
Code: Select all
var sUrl ="https://myrestapi/endpoint";
See:
https://stackoverflow.com/questions/130 ... sing-https

You also might have a cross domain problem.
https://stackoverflow.com/questions/110 ... -http-page