04-25-2024 01:48 PM
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);
});
Solved! Go to Solution.
04-26-2024 11:24 AM
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));
04-25-2024 10:34 PM
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
04-25-2024 10:35 PM
If im renember correct, the response Can only return xml ( i know the documentation says also json )
try to chance you response to xml
04-26-2024 02:37 AM - edited 04-26-2024 02:45 AM
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));
04-26-2024 05:08 AM - edited 04-26-2024 06:34 AM
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:
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>
04-26-2024 11:24 AM
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));
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide