- Mark as New
- Bookmark Message
- Subscribe to Message
- Mute Message
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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) ?
- Mark as New
- Bookmark Message
- Subscribe to Message
- Mute Message
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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
}
});
- Mark as New
- Bookmark Message
- Subscribe to Message
- Mute Message
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark Message
- Subscribe to Message
- Mute Message
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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
}
});
