function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Andee Weir 17Andee Weir 17 

Javascript restApi call to retrieve live agent session id

Hi.

I'm trying to user Live Agent's rest API to retrieve the current session id to then subsequently retrieve if any agents are online or not.  I can call the rest API using a Chrome extension called Restlet Client & see the correct response.  However when I try to make the same call using Javascript I get 'https://xxx.salesforceliveagent.com/chat/rest/System/SessionId 400 (Bad Request)' when performing the request.send().

The javascript being used, with the appropriate URLs being anonymised, is shown below.  The restApi function gets the error but the movies function, which is basically the same but to a public rest API service which requires no request headers, doesn't.  Any suggestions would be very gratefully received. 
 
<html>
    <head>
    </head>
    <body>

        <button type="submit" onclick="javascript:restApi()">RestApi</button>
        
        <button type="submit" onclick="javascript:movies()">Movies</button>
        
    
        <script type="text/javascript">
            
            function restApi(){
                var request = new XMLHttpRequest();

                request.open('GET', 'https://xxx.salesforceliveagent.com/chat/rest/System/SessionId', true);
                request.setRequestHeader('X-LIVEAGENT-API-VERSION', '34');
                request.setRequestHeader('X-LIVEAGENT-AFFINITY', '');
                request.onload = function () {
                    var data = JSON.parse(this.response);
                    console.log(request.status);
                        if (request.status >= 200 && request.status < 400) {
                            console.log('worked');
                        } else {
                            console.log('error');
                        }
                }

                request.send();
            }
            
            function movies(){
                var request = new XMLHttpRequest();

                request.open('GET', 'https://ghibliapi.herokuapp.com/films', true);
                request.setRequestHeader('X-LIVEAGENT-API-VERSION', '34');
                request.setRequestHeader('X-LIVEAGENT-AFFINITY', '');
                request.onload = function () {
                    var data = JSON.parse(this.response);
                    console.log(request.status);
                        if (request.status >= 200 && request.status < 400) {
                            console.log('worked');
                            data.forEach(movie => {
                                console.log(movie.title);
                            });
                        } else {
                            console.log('error');
                        }
                }

                request.send();
            }

            
        </script>

    </body>
<html> 
Dinesh Kumar R 7Dinesh Kumar R 7
Hi @ Andee Weir 17,
I hope you are doing good today..
We are also facing the same issue in our project. we are stuck here, not able to figure out ..  if you got any solution for this issue, please share your input and suggestion to us. it will be really helpful for us to complete our project.   You can send me an email to dineshkumar727@live.com...
Thank you in advance.  

Regards,
Dinesh kumar