HTTP status codes 415 with contact list create

Hi PureCloud team

I am trying to use python to create a new contact list.

I am using oauth example here: https://developer.mypurecloud.com/api/tutorials/oauth-client-credentials/#python

Contact list
https://developer.mypurecloud.com/api/rest/v2/outbound/index.html#postOutboundContactlists

I am using the code below, and the following is the output: Failure: 415 - Unsupported Media Type

import base64, requests, sys, json

clientId = ' '
clientSecret = ' '
authorization = base64.b64encode(clientId + ':' + clientSecret)

requestHeaders = {
_ 'Authorization': 'Basic ' + authorization,_
_ 'Content-Type': 'application/x-www-form-urlencoded'_
}
requestBody = {
_ 'grant_type': 'client_credentials'_
}

response = requests.post('https://login.mypurecloud.com.au/oauth/token', data=requestBody, headers=requestHeaders)

if response.status_code == 200:
_ print 'Got token'_
else:
_ print 'Failure: ' + str(response.status_code) + ' - ' + response.reason_
_ sys.exit(response.status_code)_

responseJson = response.json()

requestHeaders = {
_ 'Authorization': responseJson['token_type'] + ' ' + responseJson['access_token']}_

requestBody = {"name": "My Contact List","columnNames": [
_ "First Name"_
_ "Last Name",_
_ "Home",_
_ "Work",_
_ "Contact ID"_
_ ]_
_ ,_
_ "phoneColumns": [_
_ { "columnName": "Cell", type: "cell"},_
_ { "columnName": "Home", type: "home"}_
_ ]_
_ } _

response = requests.post('https://api.mypurecloud.com.au/api/v2/outbound/contactlists', data=requestBody, headers=requestHeaders)

if response.status_code == 200:
_ print 'All good'_
else:
_ print 'Failure: ' + str(response.status_code) + ' - ' + response.reason_
_ sys.exit(response.status_code)_

It would be great if you could provide some assistance.

Many thanks
Tim

It doesn't appear that you're using the Python SDK. I'm not sure what's wrong with your manual request, but this request using the Python SDK is working correctly. Give this a try:

# import SDK
import PureCloudPlatformClientV2

# (do the client credentials login here)

# Set host
PureCloudPlatformClientV2.configuration.host = 'https://api.mypurecloud.com.au'

# Set access token
PureCloudPlatformClientV2.configuration.access_token = responseJson['access_token']

# Build request
api_instance = PureCloudPlatformClientV2.OutboundApi()
body = PureCloudPlatformClientV2.ContactList() # ContactList | ContactList
body.name = "My Contact List"
body.column_names = [ "First Name", "Last Name", "Home", "Cell", "Contact ID" ]
body.phone_columns = []
col1 = PureCloudPlatformClientV2.ContactPhoneNumberColumn()
col1.column_name = "Cell"
col1.type = "cell"
body.phone_columns.append(col1)
col2 = PureCloudPlatformClientV2.ContactPhoneNumberColumn()
col2.column_name = "Home"
col2.type = "home"
body.phone_columns.append(col2)

try:
    # Create a contact List.
    api_response = api_instance.post_outbound_contactlists(body)
    print "Created contact list " + api_response.name
except Exception as e:
    print "Exception when calling OutboundApi->post_outbound_contactlists: %s\n" % e
1 Like

Thanks Tim - works perfectly!

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.