<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Posting to a REST API from a Custom Visual using TypeScript in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/Posting-to-a-REST-API-from-a-Custom-Visual-using-TypeScript/m-p/4597007#M59739</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've read that you can create custom visuals with a button that will POST to a rest API.&amp;nbsp; I want to use this to update our database via a REST API from our Power BI Report in our Embedded workspace.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've used the below TypeScript code, but each time I click the button, I get a "TypeError: Failed to Fetch" message.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can't use PowerApps to do this, as our customers use the Report and so they do not all have the logins needed for Power Apps authentication (even when using a Per App Licence).&lt;BR /&gt;&lt;BR /&gt;Any help would be much appreciated.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Lee.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;private onButtonClick(): void {&lt;BR /&gt;// Prepare the payload; include values extracted from the model&lt;BR /&gt;const payload = {&lt;/P&gt;&lt;P&gt;Value: "98"&lt;BR /&gt;};&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;const json = JSON.stringify(payload)&lt;BR /&gt;&lt;BR /&gt;this.button.innerText = json&lt;/P&gt;&lt;P&gt;const apiUrl = "&lt;A href="http://localhost:5000/log/" target="_blank" rel="noopener"&gt;http://localhost:5000/log/&lt;/A&gt;";&lt;/P&gt;&lt;P&gt;fetch(apiUrl, {&lt;BR /&gt;method: "POST"&lt;BR /&gt;,&lt;BR /&gt;headers: {&lt;BR /&gt;"Content-Type": "application/json"&lt;BR /&gt;}&lt;BR /&gt;,&lt;BR /&gt;body: json&lt;BR /&gt;})&lt;BR /&gt;.then(response =&amp;gt; {&lt;BR /&gt;if (!response.ok) {&lt;BR /&gt;this.button.innerText = `API call failed: ${response.statusText}`;&lt;BR /&gt;}&lt;BR /&gt;return response.json();&lt;BR /&gt;})&lt;BR /&gt;.then(data =&amp;gt; {&lt;BR /&gt;this.button.innerText = data;&lt;BR /&gt;})&lt;BR /&gt;.catch(error =&amp;gt; {&lt;BR /&gt;this.button.innerText = error;&lt;BR /&gt;&lt;BR /&gt;});&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 05 Mar 2025 17:13:39 GMT</pubDate>
    <dc:creator>LPAN012345</dc:creator>
    <dc:date>2025-03-05T17:13:39Z</dc:date>
    <item>
      <title>Posting to a REST API from a Custom Visual using TypeScript</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Posting-to-a-REST-API-from-a-Custom-Visual-using-TypeScript/m-p/4597007#M59739</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've read that you can create custom visuals with a button that will POST to a rest API.&amp;nbsp; I want to use this to update our database via a REST API from our Power BI Report in our Embedded workspace.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've used the below TypeScript code, but each time I click the button, I get a "TypeError: Failed to Fetch" message.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can't use PowerApps to do this, as our customers use the Report and so they do not all have the logins needed for Power Apps authentication (even when using a Per App Licence).&lt;BR /&gt;&lt;BR /&gt;Any help would be much appreciated.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Lee.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;private onButtonClick(): void {&lt;BR /&gt;// Prepare the payload; include values extracted from the model&lt;BR /&gt;const payload = {&lt;/P&gt;&lt;P&gt;Value: "98"&lt;BR /&gt;};&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;const json = JSON.stringify(payload)&lt;BR /&gt;&lt;BR /&gt;this.button.innerText = json&lt;/P&gt;&lt;P&gt;const apiUrl = "&lt;A href="http://localhost:5000/log/" target="_blank" rel="noopener"&gt;http://localhost:5000/log/&lt;/A&gt;";&lt;/P&gt;&lt;P&gt;fetch(apiUrl, {&lt;BR /&gt;method: "POST"&lt;BR /&gt;,&lt;BR /&gt;headers: {&lt;BR /&gt;"Content-Type": "application/json"&lt;BR /&gt;}&lt;BR /&gt;,&lt;BR /&gt;body: json&lt;BR /&gt;})&lt;BR /&gt;.then(response =&amp;gt; {&lt;BR /&gt;if (!response.ok) {&lt;BR /&gt;this.button.innerText = `API call failed: ${response.statusText}`;&lt;BR /&gt;}&lt;BR /&gt;return response.json();&lt;BR /&gt;})&lt;BR /&gt;.then(data =&amp;gt; {&lt;BR /&gt;this.button.innerText = data;&lt;BR /&gt;})&lt;BR /&gt;.catch(error =&amp;gt; {&lt;BR /&gt;this.button.innerText = error;&lt;BR /&gt;&lt;BR /&gt;});&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Mar 2025 17:13:39 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Posting-to-a-REST-API-from-a-Custom-Visual-using-TypeScript/m-p/4597007#M59739</guid>
      <dc:creator>LPAN012345</dc:creator>
      <dc:date>2025-03-05T17:13:39Z</dc:date>
    </item>
    <item>
      <title>Re: Posting to a REST API from a Custom Visual using TypeScript</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Posting-to-a-REST-API-from-a-Custom-Visual-using-TypeScript/m-p/4597872#M59744</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/294973"&gt;@LPAN012345&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;Based on your description and codes,&amp;nbsp;t&lt;SPAN&gt;his error is commonly related to a cross-origin issue (or similar network problems) when running your fetch() call from within a Power BI custom visual. &lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;Could you please check the following info?&lt;/SPAN&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;SPAN&gt;&lt;SPAN&gt;&lt;SPAN&gt;When your custom visual (running within Power BI or an embedded report) makes a request to your REST API (&lt;A href="http://localhost:5000" target="_blank"&gt;http://localhost:5000&lt;/A&gt; in your example), the browser enforces cross-origin restrictions. Please verify that your API is configured to allow requests from the domain hosting your Power BI report. For local development you may need to temporarily allow all origins (e.g., using the CORS middleware in your API) or whitelist the necessary domains. If you’re using an Express server in Node.js, you can add:    &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;SPAN&gt;&lt;SPAN&gt;or configure it with specific origins.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="100%"&gt;
&lt;P&gt;&lt;SPAN&gt;const cors = require('cors');    &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;app.use(cors()); &lt;/SPAN&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/LI&gt;
&lt;LI&gt;Update the URL to &lt;A href="https://localhost:5000/log/" target="_blank"&gt;https://localhost:5000/log/&lt;/A&gt; (ensure your local server supports HTTPS)&lt;/LI&gt;
&lt;LI&gt;
&lt;P&gt;&lt;SPAN&gt;Modify your&amp;nbsp;&lt;/SPAN&gt;&lt;CODE&gt;fetch&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;call to include CORS parameters&lt;/SPAN&gt;&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="100%"&gt;
&lt;P&gt;&lt;EM&gt;&lt;FONT size="3"&gt;fetch(apiUrl, {&lt;/FONT&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;FONT size="3"&gt;&amp;nbsp; method: "POST",&lt;/FONT&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;FONT size="3"&gt;&amp;nbsp; headers: { "Content-Type": "application/json" },&lt;/FONT&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;FONT size="3"&gt;&amp;nbsp; body: json,&lt;/FONT&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;FONT size="3"&gt;&amp;nbsp; mode: "cors", // Explicitly enable CORS&lt;/FONT&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;FONT size="3"&gt;&amp;nbsp; credentials: "omit" // Adjust based on your auth requirements&lt;/FONT&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;FONT size="3"&gt;})&lt;/FONT&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Best Regards&lt;/P&gt;</description>
      <pubDate>Thu, 06 Mar 2025 06:59:40 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Posting-to-a-REST-API-from-a-Custom-Visual-using-TypeScript/m-p/4597872#M59744</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2025-03-06T06:59:40Z</dc:date>
    </item>
    <item>
      <title>Re: Posting to a REST API from a Custom Visual using TypeScript</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Posting-to-a-REST-API-from-a-Custom-Visual-using-TypeScript/m-p/4598347#M59747</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the advice/help.&amp;nbsp; I did have most of that in place, but not all of it.&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;I do now have it working, and the two main things causing the issues were not using HTTPS and also not realising you have to amend the "Capabilites.json" file to include your REST API URL in the privileges section, as shown below.&lt;BR /&gt;&lt;BR /&gt;"privileges": [&lt;BR /&gt;{&lt;BR /&gt;"name": "WebAccess",&lt;BR /&gt;"essential": true,&lt;BR /&gt;"parameters": [&lt;BR /&gt;"&lt;A href="https://leetest.free.beeceptor.com" target="_blank"&gt;https://leetest.free.beeceptor.com&lt;/A&gt;",&lt;BR /&gt;]&lt;BR /&gt;}&lt;BR /&gt;]&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Mar 2025 10:38:34 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Posting-to-a-REST-API-from-a-Custom-Visual-using-TypeScript/m-p/4598347#M59747</guid>
      <dc:creator>LPAN012345</dc:creator>
      <dc:date>2025-03-06T10:38:34Z</dc:date>
    </item>
  </channel>
</rss>

