<?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 How to call custom rest api with SplunkJS SDK in app? in Splunk Dev</title>
    <link>https://community.splunk.com/t5/Splunk-Dev/How-to-call-custom-rest-api-with-SplunkJS-SDK-in-app/m-p/592317#M10509</link>
    <description>&lt;P&gt;I'm developing a new app with react js and SplunkJS SDK.&lt;/P&gt;
&lt;P&gt;In my app, I want to call a custom endpoint service that locates in the same app with a URL&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;https://x.x.x.x:8089/servicesNS/-/&amp;lt;my-app&amp;gt;/run&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In my call, I'm trying to call a custom endpoint:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;handleSplunkConnect(event) {
      let http = new splunk_js_sdk.SplunkWebHttp();       
      return new splunk_js_sdk.Service(
          http,
          application_name_space,
      );
    } 
 makeCloudFlareAction(event){
      let service = this.handleSplunkConnect() //make splunk service connect
      let params = {
        "customer": "JAUC-011",
      }
      service.post("/servicesNS/-/&amp;lt;my-app&amp;gt;/run", params, function(err, resp) {
        console.log(resp)
        console.log(err)
      });&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I see the URL request is `&lt;A target="_blank" rel="noopener"&gt;https://x.x.x.x:8000/en-US/splunkd/__raw/servicesNS/-/&amp;lt;my-app&amp;gt;/run?output_mode=json`&lt;/A&gt;&amp;nbsp;instead of `&lt;A target="_blank" rel="noopener"&gt;https://x.x.x.x:8089/servicesNS/-/&amp;lt;my-app&amp;gt;/run?output_mode=json`&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;My question is how can I call a custom endpoint service with Splunk:8089&amp;nbsp; by using SplunkJS SDK?&lt;BR /&gt;OR, Can I call a custom endpoint service with port 8080 (web) instead of 8089 (splunkd) ?&lt;/P&gt;</description>
    <pubDate>Tue, 05 Apr 2022 15:33:40 GMT</pubDate>
    <dc:creator>datphamtat</dc:creator>
    <dc:date>2022-04-05T15:33:40Z</dc:date>
    <item>
      <title>How to call custom rest api with SplunkJS SDK in app?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-call-custom-rest-api-with-SplunkJS-SDK-in-app/m-p/592317#M10509</link>
      <description>&lt;P&gt;I'm developing a new app with react js and SplunkJS SDK.&lt;/P&gt;
&lt;P&gt;In my app, I want to call a custom endpoint service that locates in the same app with a URL&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;https://x.x.x.x:8089/servicesNS/-/&amp;lt;my-app&amp;gt;/run&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In my call, I'm trying to call a custom endpoint:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;handleSplunkConnect(event) {
      let http = new splunk_js_sdk.SplunkWebHttp();       
      return new splunk_js_sdk.Service(
          http,
          application_name_space,
      );
    } 
 makeCloudFlareAction(event){
      let service = this.handleSplunkConnect() //make splunk service connect
      let params = {
        "customer": "JAUC-011",
      }
      service.post("/servicesNS/-/&amp;lt;my-app&amp;gt;/run", params, function(err, resp) {
        console.log(resp)
        console.log(err)
      });&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I see the URL request is `&lt;A target="_blank" rel="noopener"&gt;https://x.x.x.x:8000/en-US/splunkd/__raw/servicesNS/-/&amp;lt;my-app&amp;gt;/run?output_mode=json`&lt;/A&gt;&amp;nbsp;instead of `&lt;A target="_blank" rel="noopener"&gt;https://x.x.x.x:8089/servicesNS/-/&amp;lt;my-app&amp;gt;/run?output_mode=json`&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;My question is how can I call a custom endpoint service with Splunk:8089&amp;nbsp; by using SplunkJS SDK?&lt;BR /&gt;OR, Can I call a custom endpoint service with port 8080 (web) instead of 8089 (splunkd) ?&lt;/P&gt;</description>
      <pubDate>Tue, 05 Apr 2022 15:33:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-call-custom-rest-api-with-SplunkJS-SDK-in-app/m-p/592317#M10509</guid>
      <dc:creator>datphamtat</dc:creator>
      <dc:date>2022-04-05T15:33:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to call custom rest api with SplunkJS SDK in app?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-call-custom-rest-api-with-SplunkJS-SDK-in-app/m-p/592549#M10510</link>
      <description>&lt;P&gt;I found the solution, two things need to know in here:&lt;/P&gt;&lt;P&gt;For the first one,&amp;nbsp; we can call a custom service endpoint with a proxy, in this case, the proxy is Splunk web (splunk-server:8000). It means you can call&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;https://x.x.x.x:8000/en-US/splunkd/__raw/services/api-path&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Replace for rest endpoint&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;https://x.x.x.x:8089/servicesNS/-/&amp;lt;my-app&amp;gt;/api-path&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The second one, use the request function instead of the post function in SPlunkJS SDK for more customize.&lt;/P&gt;&lt;P&gt;&lt;A title="SplunkJS SDK request function detail" href="https://docs.splunk.com/DocumentationStatic/JavaScriptSDK/1.0/splunkjs.Service.html#splunkjs.Service^request" target="_blank" rel="noopener"&gt;https://docs.splunk.com/DocumentationStatic/JavaScriptSDK/1.0/splunkjs.Service.html#splunkjs.Service^request&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My sample code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;     service.request("/services/my-api-path","POST", {}, {}, JSON.stringify(params), {'Content-Type': 'application/json'}, function(err, resp) {
         // Handle response    
         if(resp != null){
           if(resp.status == 200){  
             //do something
           } else {
             //do something with status !=200
           }
         }
         // Handle error
         if(err != null){
           //handle error
         }
       });&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Apr 2022 02:10:33 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-call-custom-rest-api-with-SplunkJS-SDK-in-app/m-p/592549#M10510</guid>
      <dc:creator>datphamtat</dc:creator>
      <dc:date>2022-04-06T02:10:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to call custom rest api with SplunkJS SDK in app?</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/How-to-call-custom-rest-api-with-SplunkJS-SDK-in-app/m-p/594607#M10511</link>
      <description>&lt;P&gt;Thank you very much for the support. This helped me to figure out how to send a request from the frontend to the backend, without specifying the CSRF-token but rather using the splunk_js_sdk to do that for me.&lt;/P&gt;&lt;P&gt;I ended up with following function, to add to my api.js file.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;const postFetchData = async ( endpoint, data ) =&amp;gt; {
    // splunk_js_sdk can be imported from "splunkjs/splunk" within 'define()'
    const http = new splunk_js_sdk.SplunkWebHttp();
    const service = new splunk_js_sdk.Service( http, appName );
    const result = await service.request(
        `/services/${endpoint}`,
        'POST',
        {}, {},
        JSON.stringify( data ),
        {'Content-Type': 'application/json; charset=UTF-8'},
        ( res, err ) =&amp;gt; {
            return ( !err ) ? res : err
        }
    )
    return result
}
// make sure to be in an async context when calling 'await'
const response = await postFetchData( '&amp;lt;your-endpoint&amp;gt;', your_data )
// Following line will most likely output a STRING!
// try turning it into json inside try-catch-block.
console.log( response )&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;May be someone out there will appreciate the Copy-Pasta.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Apr 2022 08:34:19 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/How-to-call-custom-rest-api-with-SplunkJS-SDK-in-app/m-p/594607#M10511</guid>
      <dc:creator>37dmk</dc:creator>
      <dc:date>2022-04-21T08:34:19Z</dc:date>
    </item>
  </channel>
</rss>

