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!

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...

Modernize your Splunk Apps – Introducing Python 3.13 in Splunk

We are excited to announce that the upcoming releases of Splunk Enterprise 10.2.x and Splunk Cloud Platform ...

Step into “Hunt the Insider: An Splunk ES Premier Mystery” to catch a cybercriminal ...

After a whole week of being on call, you fell asleep on your keyboard, and you hit a sequence of buttons that ...