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!

Data Management Digest – December 2025

Welcome to the December edition of Data Management Digest! As we continue our journey of data innovation, the ...

Index This | What is broken 80% of the time by February?

December 2025 Edition   Hayyy Splunk Education Enthusiasts and the Eternally Curious!    We’re back with this ...

Unlock Faster Time-to-Value on Edge and Ingest Processor with New SPL2 Pipeline ...

Hello Splunk Community,   We're thrilled to share an exciting update that will help you manage your data more ...