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
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Best Practices: Splunk auto adjust pipeline queue

When you enable autoAdjustQueue in Splunk, maxSize should be understood as the queue size Splunk starts with ...

Laser Bananas and Edge Hubs: Exploring Operational Technology (OT) Data Through a ...

  OT is a different environment to traditional IT and can have interesting challenges when interfacing the ...

Event Series: Mastering AI Tokenomics and Splunk Agent Observability

Beyond the Black Box: Correlating AI Performance and Tokenomics with Splunk Agent Observability   As ...