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
Get Updates on the Splunk Community!

App Platform's 2025 Year in Review: A Year of Innovation, Growth, and Community

As we step into 2026, it’s the perfect moment to reflect on what an extraordinary year 2025 was for the Splunk ...

Operationalizing Entity Risk Score with Enterprise Security 8.3+

Overview Enterprise Security 8.3 introduces a powerful new feature called “Entity Risk Scoring” (ERS) for ...

Unlock Database Monitoring with Splunk Observability Cloud

  In today’s fast-paced digital landscape, even minor database slowdowns can disrupt user experiences and ...