cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
382
Views
0
Helpful
4
Replies
Highlighted

Using CMX REST API to add notification subscription

Hi,

I am trying to use the Cisco CMX REST API outlined here:

Cisco Connected Mobile Experiences REST API Guide, Release 10.3 - Configuration [Cisco Connected Mobile Experiences] - …

To add a notification subscription as described in the PUT: Add Notification Subscription section of the document above.


I based my solution on the Python Sample Rest Code example found here:

https://cmxlocationsandbox.cisco.com/apidocs/code


Here is what I tried:

import requests


response = requests.request(method="PUT", url="https://x.x.x.x/api/config/v1/notification", auth=("admin", "pass"), verify=False, data=[('name', 'locations'), ('userId', 'admin'),

('rules', [OrderedDict([('conditions', [OrderedDict([('condition', 'movement.distance > 5')]), OrderedDict([('condition', 'movement.deviceType == tag')])])])]),

('subscribers', [OrderedDict([('receivers', [OrderedDict([('uri', 'http://192.168.51.142:8000'), ('messageFormat', 'JSON'), ('qos', 'AT_MOST_ONCE')])])])]),

('enabled', True),

('enableMacScrambling', False),

('macScramblingSalt', '')]


However, the response I get has status code 405 with reason = "Unsupported Media Type".

I suspect something is wrong in the data portion of my request but I dont know what.

Could someone send me an example working PUT: Add Notification Subscription request?

Note that the following GET: All Notification Subscriptions request works fine:

response = requests.request(method="GET", url="https://x.x.x.x/api/config/v1/notifications", auth=("admin", "pass"), verify=False)


This correctly returns with response status code 200 and with all subscriptions in the body of the response.

I thought maybe the data portion of the PUT: Add Notification Subscription request has to be in XML format.


So I tried this:

import requests

import dicttoxml # https://pypi.python.org/pypi/dicttoxml


response = requests.request(method="PUT", url="https://x.x.x.x/api/config/v1/notification", auth=("admin", "pass"), verify=False, data=dicttoxml(json_data))


Where json_data variable is the original json data I used in my first attempt above.

But this returned response status code 400 with reason = 'Script injection detected!'

What am I missing or doing wrong in my request?

Thank you,

Ovidiu

Everyone's tags (2)
4 REPLIES 4

Re: Using CMX REST API to add notification subscription

Update:

So I managed to add a notification subscription using the Configuration API on my CMX server located here:

https://x.x.x.x/apidocs/configuration-api


Under Notification Subscription API, under the brown PUT Add notification subscription tab I put in the following:CMXAPICapture.PNG

And I got response code 201, and now when I do a GET: All Notification Subscriptions I see that my notification subscription got created successfully.


So my question becomes:

What does this API do differently in its request than my Python script?

Re: Using CMX REST API to add notification subscription

I've updated my Python request based on the following Cisco Lab:

https://learninglabs.cisco.com/lab/09-cmx-03-cmx-and-mse-deep-dive/step/3


Here is my Python request code now:


import requests

import base64


auth_header = base64.b64encode(("admin:pass").encode('utf-8'))

auth_header = "Basic " + auth_header.decode('utf-8'# 'Basic YWRtaW46UFJIaG91bGUh'

json_data =

[

('name', 'locations'),

('userId', 'admin'),

('rules', [OrderedDict([('conditions', [OrderedDict([('condition', 'movement.distance > 5')]), OrderedDict([('condition', 'movement.deviceType == tag')])])])]),

('subscribers', [OrderedDict([('receivers', [OrderedDict([('uri', 'http://192.168.51.142:8000'), ('messageFormat', 'JSON'), ('qos', 'AT_MOST_ONCE')])])])]),

('enabled', True),

('enableMacScrambling', True),

('macScramblingSalt', 'listening')

]


response = requests.request(method="PUT", url="https://x.x.x.x/api/config/v1/notification", auth=("admin", "pass"), verify=False, data=json_data,

headers={"Content-Type": "application/json", "Cache-Control": "no-cache", "Authorization": auth_header})


Now, in my response status code I get 500 with reason="Request failed." and the content of the response contains "CMX: System error"

Any idea what I might be doing wrong in my request? I tried to mimic the Learning Lab in the link above.

Note: the Learning Lab PUT Request contained a Postman-Token in the request header, but to my understanding, it's not necessary. Even so, I tried with a postman token and still get the same 500 response.

Re: Using CMX REST API to add notification subscription

Got it!

I changed the data parameter of the Python requests.request to json and the PUT request worked!

Here is the request that worked:

response = requests.request(method="PUT", url="https://x.x.x.x/api/config/v1/notification", auth=("admin", "pass"), verify=False, json=json_data)

Frequent Contributor

Re: Using CMX REST API to add notification subscription

Hello,

I moved your question to the DevNet CMX Mobility Services API discussion forum.

Sorry for the delayed response, but happy you found the solution.  Let us know if you have further questions.

Thanks.

CreatePlease to create content
Content for Community-Ad
August's Community Spotlight Awards
This widget could not be displayed.