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!

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 ...

Splunk Custom Visualizations App End of Life

The Splunk Custom Visualizations apps End of Life for SimpleXML will reach end of support on Dec 21, 2024, ...