cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
192
Views
1
Helpful
5
Replies

API call to get agent status

jfigueroa
Level 1
Level 1

Hi, i am trying to get the Finesse agent status from asp.net using javascript but all i get is server responded with a status of 415 (Unsupported Media Type).

what i got so far

 

var agentID = "user";
var finesseEndpoint = "http://mydomain.com:8082/finesse/api/User/" + agentID;
const agentStatusLabel = document.getElementById('agentStatus');

// Make a GET request to the Finesse API endpoint
fetch(finesseEndpoint, {
    method: 'GET',
    headers: {
        'Authorization': 'Basic ' + btoa(agentID + ':password'),
        'Content-Type': 'application/xml'
    }
})
    .then(response => response.json())
    .then(data => {
        // Handle the response data here
        console.log(data);

        // Extract the 'state' property from the response data
        const agentState = data.state;

        // Update the agentStatusLabel variable with the agent's state
        agentStatusLabel.innerText = agentState;
    })
    .catch(error => {
        // Handle any errors
        console.error('Error:', error);
    });

 

 

1 Accepted Solution

Accepted Solutions

got it !

complete working example

var agentID = 'agentID';
var password = 'password';
var credentials = btoa(agentID + ":" + password);

var agentStatusLabel = document.getElementById('agentStatus');
var finesseEndpoint = "http://finesse.domain.com:8082/finesse/api/User/" + agentID;

var myHeaders = new Headers();
myHeaders.append("Authorization", "Basic " + credentials);
myHeaders.append("Origin", "http://requester.domain.com");

const requestOptions = {
    method: "GET",
    headers: myHeaders,
    redirect: "follow"
};

fetch(finesseEndpoint, requestOptions)
    .then((response) => response.text())
    .then(xmlString => {
        var parser = new DOMParser();
        var xmlDoc = parser.parseFromString(xmlString, "text/xml");
        return xmlDoc.getElementsByTagName("state")[0].textContent;
    })
    .then(state => {
        console.log("Agent State:", state);
        agentStatusLabel.innerText = state;
    })
    .catch((error) => console.error(error));

View solution in original post

5 Replies 5

dekwan
Cisco Employee
Cisco Employee

Hi,

If you are doing a GET, you should not pass in the Content-Type. That is only for POST and PUT that have request bodies.

Thanx,

Denise

If im renember correct, the response Can only return xml  ( i know the documentation says also json )

try to chance you response to xml

Please rate helpful posts and if applicable mark "Accept as a Solution".
Thanks, Thomas G. J.

Hey

 

Working example :

 

 

myHeaders.append("Authorization", "Basic ' + btoa(agentID + ':password");

const requestOptions = {
  method: "GET",
  headers: myHeaders,
  redirect: "follow"
};

fetch("http://mydomain.com:8082/finesse/api/User/"+agentID, requestOptions)
  .then((response) => response.text())
  .then((result) => 
{
    var xmlDoc = xml.result;
    var x = xmlDoc.getElementsByTagName('state');
})
  .catch((error) => console.error(error));

 

 

 

Please rate helpful posts and if applicable mark "Accept as a Solution".
Thanks, Thomas G. J.

I am getting, Uncaught ReferenceError: myHeaders is not defined

i add 

 

 

 

 

 

var myHeaders = new Headers();

 

 

 

 

 

 now is the dreaded CORS error even though the request and the finesse server are on the same domain .

error:

Access to fetch at 'http://mydomain.com:8082/finesse/api/User/agentID' from origin 'http://mydomain.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

so I add 

 

 

 

myHeaders.append("Origin", "http://mydomain.com");

 

 

 

 

 

this is what i end up with:

 

 

var agentID = 'myagentID';
var password = 'mypassword';
var credentials = btoa(agentID + ":" + password);

var agentStatusLabel = document.getElementById('agentStatus');
var finesseEndpoint = "http://finesse.mydomain.com:8082/finesse/api/User/" + agentID;

var myHeaders = new Headers();
myHeaders.append("Authorization", "Basic " + credentials);
myHeaders.append("Origin", "http://server.mydomain.com");

const requestOptions = {
    method: "GET",
    headers: myHeaders,
    redirect: "follow"
};

fetch(finesseEndpoint, requestOptions)
    .then((response) => response.text())
    .then((result) => {
        var xmlDoc = xml.result;
        var agentStatus = xmlDoc.getElementsByTagName('state');
        agentStatusLabel.innerText = agentStatus;
        

    })
    .catch((error) => console.error(error));
console.log("Status: " + agentStatusLabel);

 

 

with the following error:

error.jpg

 

 

 

if i access the api directly on the browser, this is the response (http://finesse.mydomain.com:8082/finesse/api/User/agentID)

 

<User>
<dialogs>/finesse/api/User/agentID/Dialogs</dialogs>
<extension/>
<firstName>agentID FirstName</firstName>
<lastName>agentID LastName</lastName>
<loginId>agentID</loginId>
<loginName>agentID</loginName>
<mediaType>1</mediaType>
<pendingState/>
<reasonCodeId>-1</reasonCodeId>
<roles>
<role>Agent</role>
<role>Supervisor</role>
</roles>
<settings>
<wrapUpOnIncoming/>
</settings>
<state>LOGOUT</state>
<stateChangeTime/>
<teamId>1</teamId>
<teamName>Default</teamName>
<teams>
<Team>
<id>2</id>
<name>GroupName</name>
<uri>/finesse/api/Team/2</uri>
</Team>
</teams>
<uri>/finesse/api/User/agentID</uri>
</User>

 

 

got it !

complete working example

var agentID = 'agentID';
var password = 'password';
var credentials = btoa(agentID + ":" + password);

var agentStatusLabel = document.getElementById('agentStatus');
var finesseEndpoint = "http://finesse.domain.com:8082/finesse/api/User/" + agentID;

var myHeaders = new Headers();
myHeaders.append("Authorization", "Basic " + credentials);
myHeaders.append("Origin", "http://requester.domain.com");

const requestOptions = {
    method: "GET",
    headers: myHeaders,
    redirect: "follow"
};

fetch(finesseEndpoint, requestOptions)
    .then((response) => response.text())
    .then(xmlString => {
        var parser = new DOMParser();
        var xmlDoc = parser.parseFromString(xmlString, "text/xml");
        return xmlDoc.getElementsByTagName("state")[0].textContent;
    })
    .then(state => {
        console.log("Agent State:", state);
        agentStatusLabel.innerText = state;
    })
    .catch((error) => console.error(error));