All Apps and Add-ons

How to pass tokens in url using the new web framework

adityapavan18
Contributor

I have built a app using django & js in splunk web framework.
The home screen gives running status of servers.On click i should pass that server name to a different view .

And then i should be able to take that token from URL and substitute in search of new view.

I know to use it using url redirector in advanced dashboards, but couldn't find any documentation on how to pass values to other views and use in new splunk web framework

It would be of great if someone could help me in this?

0 Karma

peter_krammer
Communicator

Today I faced a similar challenge, but I did not want to specifically set all possible tokens.
So I wrote it in a generic way so that all params are set as tokens in my django dashboard.
The function getQueryParams was inspired by the following Stackoverflow answer: stackoverflow.com/a/1099670

EDIT: Just Updated it, so it can handle arrays for my CheckboxGroupView to work correctly.
Example URL: localhost:8000/dj/en-us/myDjangoApp/page1/?example_checkbox_token=[2,4,5]

{% block js %}
    <script>
        var deps = [
            "splunkjs/ready!"
        ];
        require(deps, function(mvc) {
            function getQueryParams(qs) {
                qs = qs.split("+").join(" ");

                var params = {};
                var tokens;
                var i=0;
                var re = /[?&]?([^=]+)=([^&]*)/g;

                while (tokens = re.exec(qs)) {
                    params[i] = {};
                    params[i].name = decodeURIComponent(tokens[1]);
                    var param_value = decodeURIComponent(tokens[2]);
                    if(param_value.search(/\[.*\]/) >= 0) { // then it's an array
                         params[i].value = JSON.parse(param_value);
                    } else {
                         params[i].value = param_value;
                    }
                    params[params[i].name] = params[i].value;
                    i++;
                }
                params.length = i;
                return params;
            };
            var params = getQueryParams(document.location.search);
            for (var i=0;i<params.length;i++) {
                splunkjs.mvc.Components.getInstance("default").set(params[i].name, params[i].value);
            };
        });
    </script>
{% endblock js %}

MaverickT
Communicator

I've tryed using this javascript

var split = location.search.replace('?', '').split('=');
if(split.length > 1)
{
var hostname = split[1];
var tokens = splunkjs.mvc.Components.getInstance("default");
tokens.set('host', hostname);
}

But unfortunatly the line in which I set "host" token throws exception

TypeError: Cannot read property 'text' of null

The wierd thing is, if I rename this token to something else exception doesn't happen. On the same dashboard I have dropdown that should be used if host is not passed by URL or if user wants to check data from some other host.
And another thing - despite that the exception happens - token still somehow works in only one searchmanager (it uses search macro and than the results are taken into few postprocessmanagers). There are still 4 additional searchmanagers but it doesnt affect them.

0 Karma

MaverickT
Communicator

Yes, I got it, it works now.

0 Karma

ineeman
Splunk Employee
Splunk Employee

@aelliott and @MaverickT: did you get what you needed, or is there anything we can help with?

aelliott
Motivator

Ah it probably was a caching issue 🙂

MaverickT
Communicator

Nope, it is ok. When I make alert(hostname) it alerts the value. I've given it second (or 10th :P) look after posting and if I remove dropdown object the damn THING STARTS WORKING :). So I will only have to implement how the dropdown changes token, but I think that won't be a big deal. So the case is more or less closed 🙂

0 Karma

aelliott
Motivator

Is hostname coming back as null? perhaps split is 0 based? so it will need to be split[0]?

0 Karma

dfoster_splunk
Splunk Employee
Splunk Employee

There isn't an out-of-the-box solution for passing tokens through the URL yet, but you can implement your own:

Assuming you have a view A that is trying to pass a value to view B:

  • On view A redirect to view B with ?mytoken=myvalue at the end of the URL.
  • On view B extract the value from the current URL query and set the on-page token using code like:

    splunkjs.mvc.Components.get('default').set('mytoken', decodedUrlParamValue)

Get Updates on the Splunk Community!

ICYMI - Check out the latest releases of Splunk Edge Processor

Splunk is pleased to announce the latest enhancements to Splunk Edge Processor.  HEC Receiver authorization ...

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...

Introducing the 2024 Splunk MVPs!

We are excited to announce the 2024 cohort of the Splunk MVP program. Splunk MVPs are passionate members of ...