Splunk Dev

How to call custom rest api with SplunkJS SDK in app?

datphamtat
Explorer

I'm developing a new app with react js and SplunkJS SDK.

In my app, I want to call a custom endpoint service that locates in the same app with a URL

 

 

 

https://x.x.x.x:8089/servicesNS/-/<my-app>/run

 

 

 

In my call, I'm trying to call a custom endpoint:

 

 

 

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/-/<my-app>/run", params, function(err, resp) {
        console.log(resp)
        console.log(err)
      });

 

 

 

But I see the URL request is `https://x.x.x.x:8000/en-US/splunkd/__raw/servicesNS/-/<my-app>/run?output_mode=json` instead of `https://x.x.x.x:8089/servicesNS/-/<my-app>/run?output_mode=json`

My question is how can I call a custom endpoint service with Splunk:8089  by using SplunkJS SDK?
OR, Can I call a custom endpoint service with port 8080 (web) instead of 8089 (splunkd) ?

Labels (2)
Tags (1)
0 Karma
1 Solution

datphamtat
Explorer

I found the solution, two things need to know in here:

For the first one,  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

 

https://x.x.x.x:8000/en-US/splunkd/__raw/services/api-path

 

 Replace for rest endpoint

 

https://x.x.x.x:8089/servicesNS/-/<my-app>/api-path

 

The second one, use the request function instead of the post function in SPlunkJS SDK for more customize.

https://docs.splunk.com/DocumentationStatic/JavaScriptSDK/1.0/splunkjs.Service.html#splunkjs.Service... 

My sample code:

 

     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
         }
       });

 

 

 

 

View solution in original post

0 Karma

37dmk
Explorer

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.

I ended up with following function, to add to my api.js file.

 

const postFetchData = async ( endpoint, data ) => {
    // 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 ) => {
            return ( !err ) ? res : err
        }
    )
    return result
}
// make sure to be in an async context when calling 'await'
const response = await postFetchData( '<your-endpoint>', your_data )
// Following line will most likely output a STRING!
// try turning it into json inside try-catch-block.
console.log( response )

 

May be someone out there will appreciate the Copy-Pasta.

0 Karma

datphamtat
Explorer

I found the solution, two things need to know in here:

For the first one,  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

 

https://x.x.x.x:8000/en-US/splunkd/__raw/services/api-path

 

 Replace for rest endpoint

 

https://x.x.x.x:8089/servicesNS/-/<my-app>/api-path

 

The second one, use the request function instead of the post function in SPlunkJS SDK for more customize.

https://docs.splunk.com/DocumentationStatic/JavaScriptSDK/1.0/splunkjs.Service.html#splunkjs.Service... 

My sample code:

 

     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
         }
       });

 

 

 

 

0 Karma
Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...