All Apps and Add-ons

How to extract REST API returns token in response header

ansif
Motivator

alt textStatus
200 OK Show explanation Loading time: 5592
Response headers
Via: 1.1 Secure Proxy
Connection: Keep-Alive
Proxy-Connection: Keep-Alive
Transfer-Encoding: chunked
Date: Mon, 03 Feb 2014 06:01:39 GMT
Content-Type: application/vnd.brocade.networkadvisor+json;version="v1"
Server: Apache-Coyote/1.1
WStoken: ghe/4Q//I0EJcxD6UPdO9/fvI94=
Raw
JSON
The JSON content of the response should look like the following:
{
serverName: "DCM-x3650-103"
serverIp: "10.24.48.103"
}

Using REST API Python Script how can I extract token from response header.

class BNATokenKeyAuth(AuthBase):
        def __init__(self,**args):
                self.auth_url = args['auth_url']
                pass

        def __call__(self, r):
                if not 'WStoken' in r.headers:
                #perform auth
                        data = {'WSUsername':'******','WSPassword':'********','clientContext':'1'}
                        req_args = {"verify" : False}
                        headers = {'Accept': 'application/vnd.brocade.networkadvisor+json;version=v1'}
                        auth_response = requests.post(self.auth_url,data=json.dumps(data),headers=headers,**req_args)
                        response_json = json.loads(auth_response.text)
                        token_key = response_json["WStoken"]
                        r.headers['WStoken'] = token_key
                        return r

This is not working because JSON response doesnt have token instead the response header has.

Help me to get the token key.

Tags (1)
0 Karma

pruthvikrishnap
Contributor

Hi Ansif,

Yes, you will have to mention this in tokens.py (/rest_AT/bin/tokens.py) and specify that value in your inputs.

Let me know if this works.

0 Karma

ansif
Motivator

@pruthvikrishnapolavarapu : Thanks for your response.I am not clear on this.Please find the attachment above. Does it mean the Token is in URL and I can exttract using tokens.py?If so can you please help me in the script below and how this script invokes?

import datetime

def sometoken():
    return 'zoo'

def sometokenlist():
    return ['goo','foo','zoo']

def datetoday():
    today = datetime.date.today()
    return today.strftime('%Y-%m-%d')
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!

[Puzzles] Solve, Learn, Repeat: Matching cron expressions

This puzzle (first published here) is based on matching timestamps to cron expressions.All the timestamps ...

Design, Compete, Win: Submit Your Best Splunk Dashboards for a .conf26 Pass

Hello Splunkers,  We’re excited to kick off a Splunk Dashboard contest! We know that dashboards are a primary ...

May 2026 Splunk Expert Sessions: Security & Observability

Level Up Your Operations: May 2026 Splunk Expert Sessions Whether you are refining your security posture or ...