PAL REST API - Sample program 1
Try PAL REST API with the sample script file “pal_rest.html“.
Step 1. Copy the file “pal_rest.html” to your test web server, and put the file on an appropriate location so that you can access to the file from browser with the following URL.
https://<Your test webserver address>:<web port>/pal_rest.html
Step 2. Enter your PBX’s PAL URL.
http(s)://<PBX host address>:<port>/pbx/api/pal
Step 3. Enter the account information (tenant, user, and password).
Step 4. Click “Login” button. Internally, the function login() is executed and a POST request is sent to the PBX. Then a response that includes token will be returned.
Step 5. After login, You can call any methods. Internally the function call_method() is called, and the POST request that includes the token is sent to the PBX.
<pal_rest.html>
<!DOCTYPE html> <html> <head> <title>PAL REST API Example</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script> <!-- var token = null; var baseUrl = ""; function init(){ document.getElementById( "url" ).value = location.protocol + "//" + location.host + "/pbx/api/pal"; } function login(){ var param = { tenant : document.getElementById( "tenant" ).value, login_user : document.getElementById( "user" ).value, login_password : document.getElementById( "password" ).value, }; baseUrl = document.getElementById( "url" ).value; fetch( baseUrl + "/login", { mode: 'cors', method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify( param ) }).then( function( response ){ return response.json(); }).then( function( json ){ token = json.token; document.getElementById( "btn_call_method" ).disabled = false; }).catch( function( e ){ console.log( e ); }); } function call_method(){ fetch(baseUrl + "/" + document.getElementById( "method" ).value, { mode: 'cors', method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization' : "basic " + token }, body: document.getElementById( "params" ).value }).then( function( response ){ if( response.status != 200 ){ console.error( response ); } return response.json(); }).then( function( json ){ document.getElementById( "response" ).innerHTML = JSON.stringify( json ); }).catch( function( e ){ console.log( e ); }); } //--> </script> </head> <body onload="init();" > <form name="frm1" action="" id="frm1" style="display:block;" autocomplete="off" > <table> <tr> <td>URL</td><td> <input type="text" name="url" id="url" value="" ></td> </tr> <tr> <td>Tenant:</td><td><input type="text" name="tenant" id="tenant" value="" ></td> </tr> <tr> <td>User:</td><td><input type="text" name="user" id="user"></td> </tr> <tr> <td>Password:</td><td> <input name="password" id="password" type="password" value="" ></td> </tr> </table><br> <button type="button" onclick="login()">Login</button> </form> <br /> <form> <input type="text" name="method" id="method" value="getExtensions" ><br /> <textarea name="params" id="params" cols=20 rows=8 >{}</textarea><br /> <input type="button" disabled="true" id="btn_call_method" onclick="call_method();" value="call method" /> </form> <br > <div id="response" ></id> </body> </html>
Related Links:
How to call PAL methods via REST API >>