I am developing a Grafana panel plugin (version 9.4.7), and I need to make a fetch request. However, I want to ensure that the request goes through the datasource to avoid CORS (Cross-Origin Resource Sharing) issues. The built-in fetch method in web browsers is not the optimal approach in this case.
To achieve the desired outcome, the recommended way to make a fetch request in a Grafana panel plugin is by utilizing the getBackendSrv() function from the @grafana/runtime package. You can use this function, along with the desired HTTP method (such as GET), to interact with the datasource.
However, it is not working for me.
Here’s an simple example of how to make a fetch request through the datasource using getBackendSrv() at panel plugin:
the query is not the full path. e.g.: /target/query/at/timestamp
the datasource that I had added at grafana plugin has the path: e.g.: http://ip-address:8000/
So, I expected Grafana to receive the query and make a request through the mentioned datasource. However, it doesn’t work as expected. It behaves the same way as if I had only used the fetch method.
How to fix it? Should I use another method or another way to fetch data through the datasource?
P.s: I also tried the await getBackendSrv().fetch(…) without success…
Any help would be greatly appreciated! Thank you very much in advance!
Thanks, Yosiasz. I really liked this plugin; however, I’m not sure if it can help me. Yes, I can add it to my datasource and fetch data from it. However, I must before to set up the URL and other things in the datasource UI. In my use case, I need to be able to change the URL on the fly. The user won’t be able to edit the datasource and so on. Regardless, I can use this plugin for other requirements. Thanks!
Thank you very much but it did not work also… The second option solution: The datasourceRequest is outdated, and I tried to adapt your example to use the GET method, but I’m still encountering the same error as before. The first option was very promising, but I’m not sure where to add my URL. For example, when you retrieve the data source, how do I fetch from there?
Thanks, Yosiasz. I really liked this plugin; however, I’m not sure if it can help me. Yes, I can add it to my datasource and fetch data from it. However, I must before to set up the URL and other things in the datasource UI. In my use case, I need to be able to change the URL on the fly. The user won’t be able to edit the datasource and so on. Regardless, I can use this plugin for other requirements. Thanks!