cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Announcements

428
Views
0
Helpful
1
Replies
Mike Hagans
Beginner

Stealthwatch API calls return html

Hi all,

I'm getting responses back in HTML instead of json when making a few API calls, such as 'get users' or 'get tenants'. I'm able to do a 'get domains' api call and get back json just fine so I'm unclear what the difference is. I've tried setting my headers to accept json back but the api likes plain/text. Any help would be appreciated. Here is my class below in Python:

class StealthwatchProxy:
    """Service proxy library for interfacing with Stealthwatch SMC"""

    def __init__(self, username, password, ip
        self._username = username
        self._password = password
        self.logger = logging.getLogger(__name__)
        self._session = requests.Session()
        self.login_url = f'https://{ip}/token/v2/authenticate'
        self.get_domain_url = f'https://{ip}/sw-reporting/v1/tenants'
        self.logout_url = f'https://{ip}/token'
        self.get_users_url = f'https://{ip}/users'
        self._verifyssl = False
        self._headers = {'Accept': 'application/json'}

    def login(self
        """Tests logging into SMC"""
        self.logger.info('Performing Login')
        login_resp = self._session.post(
            self.login_url,
            data={'username': self._username,
                  'password': self._password},
            verify=self._verifyssl
        )
        cookies = [x for x in login_resp.cookies if x.name ==
                   'stealthwatch.jwt']
        if login_resp.status_code == 200 and len(cookies) == 1:
            return cookies[0].value
        raise Exception('Authentication Failed')


    def get_domains(self
        """Test can get domains from SMC"""
        self.logger.info('Get Domains called')
        resp = self._session.get(
            self.get_domain_url,
            verify=self._verifyssl
        )
        return resp.json()


    def logout(self) -> bool:
        """Logs out of SMC"""
        self.logger.info('Log out called')
        resp = self._session.delete(
            self.logout_url
        )
        return resp.status_code == 204

    def get_users(self 
        """Get Users from SMC"""
        self.logger.info('Get Users called')
        resp = self._session.get(
            self.get_users_url,
            verify=self._verifyssl
        )
        return resp.json()
1 ACCEPTED SOLUTION

Accepted Solutions
hanjabbo
Cisco Employee

HI @Mike Hagans 

 

I am not sure what is exactly wrong with this class, I can only see the definition but now they are called.

for better understanding please refer to some example scripts in github with the link below :

https://github.com/CiscoDevNet/stealthwatch-enterprise-sample-scripts/tree/master/python

Please note that starting SNA version 7.3.1 there is 2 tokens returned after authentication and they are required to be used for the other cals.

View solution in original post

1 REPLY 1
hanjabbo
Cisco Employee

HI @Mike Hagans 

 

I am not sure what is exactly wrong with this class, I can only see the definition but now they are called.

for better understanding please refer to some example scripts in github with the link below :

https://github.com/CiscoDevNet/stealthwatch-enterprise-sample-scripts/tree/master/python

Please note that starting SNA version 7.3.1 there is 2 tokens returned after authentication and they are required to be used for the other cals.

Create
Recognize Your Peers