cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1345
Views
0
Helpful
5
Replies

DNAC API to create reports failing

toddhsimmons
Level 1
Level 1

I have been trying for hours to use the reports API to create a report in DNAC.  I have followed the guidelines to a T, but I get error 500 no matter what.  I even used the "Inspect" tool with Chrome and created a report using the GUI, but the format still gave me an error 500.  Here is my code:

import requests
from requests import packages
from pprint import pprint

token = "Token goes Here"
dnacUrl = "https://DNAC IP Here/dna/intent/api/v1/data/reports"

requests.packages.urllib3.disable_warnings()
payload = {
    "name": "Todd Client Session",
    "dataCategory": "Client",
    "tags": [],
    "viewGroupId": "d7afe5c9-4941-4251-8bf5-0fb643e90847",
    "schedule": {
        "type": "SCHEDULE_RECURRENCE",
        "timeZoneId": "America/Phoenix",
        "type": "SCHEDULE_RECURRENCE",
        "scheduledServiceId": "a1b6f4a3-367c-42b4-9d00-b30d339bfbf5",
        "startDate": 1680594019999,
        "endDate": 0,
        "endAfterOccurrences": 0,
        "time": 1680594019999,
        "recurrence": {
            "days": [
                "MONDAY",
                "TUESDAY",
                "WEDNESDAY",
                "THURSDAY",
                "FRIDAY",
                "SATURDAY",
                "SUNDAY"
            ]
        }
    },
    "deliveries": [],
    "view": {
        "name": "Client Session",
        "description": "This client session report view provides detailed information about the wireless client sessions seen in the network",
        "fieldGroups": [
            {
                "fieldGroupName": "client_sessions",
                "fieldGroupDisplayName": "Client Session",
                "fields": [
                    {
                        "name": "macAddress",
                        "displayName": "Client MAC Address"
                    },
                    {
                        "name": "sessionStartTime",
                        "displayName": "Start Time"
                    },
                    {
                        "name": "sessionEndTime",
                        "displayName": "End Time"
                    },
                    {
                        "name": "duration",
                        "displayName": "Duration(ms)"
                    },
                    {
                        "name": "apMac",
                        "displayName": "AP MAC Address"
                    },
                    {
                        "name": "ssid",
                        "displayName": "SSID"
                    },
                    {
                        "name": "siteHierarchy",
                        "displayName": "Location"
                    }
                ]
            }
        ],
        "filters": [
            {
                "type": "REGULAR",
                "name": "Location",
                "displayName": "Location",
                "type": "MULTI_SELECT_TREE",
                "value": [
                    {
                        "value": "",
                        "displayValue": "Global"
                    }
                ]
            },
            {
                  "type": "SINGLE_INPUT",
                   "name": "clientMacAddresses",
                   "displayName": "client MACs (try to limit to 100 MACs Comma-separated)",
                   "type": "SINGLE_INPUT",
                   "value": ""
            },
            {
                "type": "TIME_RANGE",
                "name": "TimeRange",
                "displayName": "Time Range",
                "type": "TIME_RANGE",
                "value": {
                    "timeRangeOption": "LAST_24_HOURS",
                    "startDateTime": 0,
                    "endDateTime": 0,
                    "timeZoneId": "America/Phoenix"
                },
                "timeOptions": None
            }
        ],
        "format": {
            "name": "JSON",
            "formatType": "JSON"
        },
        "viewInfo": None,
        "viewId": "f87c7121-c1cc-419d-8032-11d950dddddd"
    },
}
   


headers = {
    "x-auth-token": token,
    "Content-Type": "application/json",
    "Accept": "application/json"
}

response = requests.request('POST', dnacUrl, headers=headers, data = payload, verify= False)

print(response.text.encode('utf8'))
 
 
>>>OUTPUT<<<
b'Http Error Code:500'
 

 

1 Accepted Solution

Accepted Solutions

toddhsimmons
Level 1
Level 1

RESOLVED

Looks like the API document is not 100% accurate.  When using Python Requests, you must turn the payload dict into JSON:

response = requests.request('POST', dnacUrl, headers=headers, data = json.dumps(payload), verify= False)
 
As soon as I made that change the code was able to create reports.

View solution in original post

5 Replies 5

toddhsimmons
Level 1
Level 1

RESOLVED

Looks like the API document is not 100% accurate.  When using Python Requests, you must turn the payload dict into JSON:

response = requests.request('POST', dnacUrl, headers=headers, data = json.dumps(payload), verify= False)
 
As soon as I made that change the code was able to create reports.

Herbert Baerten
Cisco Employee
Cisco Employee

Hi Todd (I guess)

happy to see you found the solution, and thanks for sharing it here. Could you clarifiy which API document you consider to be not accurate please, so we can review and update it?

regards

Herbert

HI Herbert, my code required the payload to be in JSON format.  I have copied and pasted the temple from the API documentation.  The way it is listen here is as a Python dictionary.  I fixed that with this:  data = json.dumps(payload)
import requests
url = "https:///dna/intent/api/v1/data/reports"
payload = '''{
"schedule": {},
"view": { "format": {} }
}'''
headers = {
"Content-Type": "application/json",
"Accept": "application/json"
}
response = requests.request('POST', url, headers=headers, data = payload)
print(response.text.encode('utf8'))
 

Herbert Baerten
Cisco Employee
Cisco Employee

Hi Todd,

ok it wasn't clear to me at first which doc you meant, but I see now it's the "python-request" template at https://developer.cisco.com/docs/dna-center/#!create-or-schedule-a-report
(and by extention, probably all the templates for all the POST API calls, not only this one).
I'll see if I can find the right person to fix this. Thanks for reporting it!

Herbert

Todd,

 

on second thought, I believe the documentation is actually correct. The payload object is a string (containing JSON), not a dict.

I suspect you may have accidentally left out the quotes ? I.e. if you write it as:

payload = {
"schedule": {},
"view": { "format": {} }
}

then payload is a dict and you need to use json.dumps(payload) to turn it into a json string.

But in the documentation, payload is a multi-line string:

payload = '''{
"schedule": {},
"view": { "format": {} }
}'''

So while this looks very similar to the dict, it is actually a valid JSON string.

Herbert