cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
710
Views
3
Helpful
5
Replies

Python Using JSON API to UCCX

bunjiega
Level 1
Level 1

Was curious if anyone has an example or any success with using JSON to interact with the UCCX REST API.

I am following this guide: https://developer.cisco.com/docs/contact-center-express/#!modify-application/modify-application 
It says it supports both XML and JSON, but I can't get JSON to work, XML works ok even though you have to send the whole configuration it seems, you can't just modify one field alone. However, I can't get JSON to work at all.

I tested using the Cisco sandbox and the main thing I want to do is update a script variable string field that the application uses.

 

Code:

 

import requests
from requests.auth import HTTPBasicAuth
import xmltodict
import json
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

username = '*********'
password = '*********'

xml_str = """<application>
	<ScriptApplication>
		<script>SCRIPT[override_script.aef]</script>
		<scriptParams>
			<name>override_status</name>
			<value>"open"</value>
			<type>java.lang.String</type>
		</scriptParams>
	</ScriptApplication>
	<id>1</id>
	<applicationName>override</applicationName>
	<type>Cisco Script Application</type>
	<description>mydescription</description>
	<maxsession>10</maxsession>
	<enabled>true</enabled>
</application>"""

def convert_xml_to_json(xml_str):
    x = xmltodict.parse(xml_str)
    return x
myjson = convert_xml_to_json(xml_str)
print(json.dumps(myjson, indent=4))

# Update using XML
def change_application_parameter_xml():
    headers = {"Accept": "application/xml", "Content-Type": "application/xml"}
    url = "https://hq-uccx.abc.inc/adminapi/application/override"
    res = requests.put(url, auth=HTTPBasicAuth(username, password), headers=headers, data=xml_str, verify=False)
    print('XML Done')
    print(res.text)
change_application_parameter_xml()

# Update using JSON
def change_application_parameter_json():
    headers = {"Accept": "application/json", "Content-Type": "application/json"}
    url = "https://hq-uccx.abc.inc/adminapi/application/override"
    res = requests.put(url, auth=HTTPBasicAuth(username, password), headers=headers, json=myjson, verify=False)
    print('JSON Done')
    print(res.json())
change_application_parameter_json()

 

Result:

 

MacBook-Pro ~ % /usr/local/bin/python3 /Users/Desktop/uccx_testing.py
{
    "application": {
        "ScriptApplication": {
            "script": "SCRIPT[override_script.aef]",
            "scriptParams": {
                "name": "override_status",
                "value": "\"open\"",
                "type": "java.lang.String"
            }
        },
        "id": "1",
        "applicationName": "override",
        "type": "Cisco Script Application",
        "description": "mydescription",
        "maxsession": "10",
        "enabled": "true"
    }
}
XML Done

JSON Done
{'apiError': [{'errorData': '', 'errorType': 'InvalidInput'}]}
MacBook-Pro ~ %

 

 

Thanks!

 

 

1 Accepted Solution

Accepted Solutions

Hey.

i have never got it to work with json payload.

an yes -  you have to send the whole payload in you request

Please rate helpful posts and if applicable mark "Accept as a Solution".
Thanks, Thomas G. J.

View solution in original post

5 Replies 5

Hey.

i have never got it to work with json payload.

an yes -  you have to send the whole payload in you request

Please rate helpful posts and if applicable mark "Accept as a Solution".
Thanks, Thomas G. J.

Thanks for the conformation on both.

My preference has been to use the etree functions from the lxml library. There are functions to convert the etree back to XML to send back to the UCCX host.

Thank you for this idea Elliot, I'll have to try this out.

I think at one point I got one or two to work, but it was very inconsistent and not worth the trouble. The XML schema is documented and really not that bad.

david