cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2887
Views
15
Helpful
3
Comments
tahanson
Cisco Employee
Cisco Employee

Uploading a remote file to a Cisco Spark room using a web-accessible URL is fairly self explanatory -  just supply the URL in the “files” field of a create message request: https://developer.ciscospark.com/endpoint-messages-post.html

But this only works for files that are on the public Internet and can be downloaded by Spark's servers. Spark pulls the files down. However, if you want to push the file to Spark, for example to upload a file from your local hard drive, it gets a little more complicated.

We’re making all of that easier now because we now support file upload via the Messages API. This means you no longer have to put your files on the public Internet for them to be attached.

How it Works:

The Spark API accepts file uploads as a MIME upload in the same way your web browser would upload a file in a web form. The two most important aspects are 1) to name the field to which you send "files" and 2) to set your Content-Type header to be multipart/form-data including a boundary.

The boundary is part of the MIME specification and is essentially any string that won't appear in the binary encoding of the file; it’s used by the server to determine where the file content ends and the request starts.

The final header would look something like this:

Content-Type: multipart/form-data; boundary=12345678901234567890123456789012

In Python, it’s difficult to do everything required without the help of a couple of third party libraries.  We’ll use requests (http://docs.python-requests.org/en/master/user/install/#install) and requests-toolbelt (https://github.com/sigmavirus24/requests-toolbelt).

pip install requests

pip install requests-toolbelt

Once you have those installed, the script itself is simple.

from requests_toolbelt import MultipartEncoder

import requests

filepath    = '/Users/taylorhanson/Desktop/screenshot.png'

filetype    = 'image/png'

roomId      = 'SOME ROOM'

token       = 'YOUR ACCOUNT BEARER TOKEN'

url         = "https://api.ciscospark.com/v1/messages"

my_fields={'roomId': roomId,

           'text': 'Hello World',

           'files': ('screenshot', open(filepath, 'rb'), filetype)

           }

m = MultipartEncoder(fields=my_fields)

r = requests.post(url, data=m,

                  headers={'Content-Type': m.content_type,

                           'Authorization': 'Bearer ' + token})

print r.json()

Above, you’ll need to replace the variables with ones suitable for you, with the exception of the url.  The filetype and filepath are totally dependent on the file you are attempting to upload - a full path to the file is expected, and the first slash in the above filepath value represents root, so it’s “root” followed by ‘/Users/taylorhanson/Desktop/screenshot.png'. The roomId value can be found from the developer portal: https://developer.ciscospark.com/endpoint-rooms-get.html

Your bearer token can be your personal one for testing, or one for a specific user via integrations: https://developer.ciscospark.com/authentication.html

Or a bot: https://developer.ciscospark.com/add-app.html

The complete code for the upload can be found on Github.

If you have any questions, please contact devsupport@ciscospark.com 24/7/365 - we’re happy to help!

Taylor Hanson, Customer Support Engineer II

3 Comments
Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: