Using Rest API in Zimlet

Have a great idea for extending Zimbra? Share ideas, ask questions, contribute, and get feedback.
Post Reply
servanvetes
Posts: 8
Joined: Sun Jan 14, 2024 11:50 pm

Using Rest API in Zimlet

Post by servanvetes »

Hello,
I am developing Zimlet and I want to enable it to communicate with the API I created.
Api classically fetch(url).then(response =>
Zimlet doesn't work when you do it with , but when you do it with the XMLHttpRequest method, the api works.
Is there a restriction here and do you have a suggested method for communicating with the rest api?
Have a nice day
saket.patel
Zimbra Employee
Zimbra Employee
Posts: 158
Joined: Mon Apr 11, 2022 8:39 pm

Re: Using Rest API in Zimlet

Post by saket.patel »

There is no restriction put on what you use in a zimlet, either fetch or xmlhttprequest both should work.
servanvetes
Posts: 8
Joined: Sun Jan 14, 2024 11:50 pm

Re: Using Rest API in Zimlet

Post by servanvetes »

Hello,
I made the fetch using the command I implemented in Javascript, but the zimlet did not work. I also look at the latest zimlets and I did not see the one made with fetch (for Zimbra 8.8.15 version).
Is there a working example code related to fetch that you can show?
Have a nice day
saket.patel
Zimbra Employee
Zimbra Employee
Posts: 158
Joined: Mon Apr 11, 2022 8:39 pm

Re: Using Rest API in Zimlet

Post by saket.patel »

I don't have anything readily available, but if you can share a small zimlet which shows your problem then we could try to run and fix it.
servanvetes
Posts: 8
Joined: Sun Jan 14, 2024 11:50 pm

Re: Using Rest API in Zimlet

Post by servanvetes »

Hello,
The method I did with XmlHttpRequest is the method I tried to do with fetch.
Here, it doesn't work when you do it with fetch, but it works when you do it with XmlHttpRequest.

var xhr = new XMLHttpRequest();
var proxyUrl = "http://localhost:5555/api/Content";
xhr.open("POST", proxyUrl);
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.onerror = function (err) {
console.error(err);
return false;
};
xhr.send(sendData);
xhr.onreadystatechange = function (oEvent) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
var response = JSON.parse(xhr.response);
console.log(response);
return response;
}
}
}


var proxyUrl = "http://localhost:5555/api/Content";
fetch(proxyUrl, {
method: 'POST', // İstek metodu
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(sendData),
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json(); //
})
.then(data => console.log(data))
.catch(error => console.error('Fetching Error:', error));
Damini Vashishtha
Zimbra Employee
Zimbra Employee
Posts: 38
Joined: Tue Oct 18, 2022 12:16 pm

Re: Using Rest API in Zimlet

Post by Damini Vashishtha »

Ensure that your API server is configured to allow requests from the domain where your Zimlet is hosted. XmlHttpRequest might be less strict about CORS, whereas fetch follows CORS rules more strictly. Check the CORS headers returned by your server.

Also, If your Zimlet relies on third-party libraries or frameworks, make sure they are compatible with fetch and aren't causing any conflicts.
Post Reply