Showing results for 
Search instead for 
Did you mean: 
Walkthrough Wednesdays

Uploading Local Files to Spark

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:

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 ( and 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'


url         = ""

my_fields={'roomId': roomId,

           'text': 'Hello World',

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


m = MultipartEncoder(fields=my_fields)

r =, 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:

Your bearer token can be your personal one for testing, or one for a specific user via integrations:

Or a bot:

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

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

Taylor Hanson, Customer Support Engineer II

Cisco Employee

Hi Taylor!

great post.  I have no problems uploading a single screen shot.  The "files" key would indicate that we can upload multiple files.  However I'm having issues doing that.  Any suggestions?  Here is what I've tried:

'files': [('screenshot', open(filepath1, 'rb'), filetype),('screenshot', open(filepath2, 'rb'), filetype)]



Cisco Employee

+1 on great post.

I'm doing a post image from a Raspberry Pi using the "picamera" in python to capture the image and set it to a PNG. It worked with JPG also.

PiCamera Python code

from time import sleep

from picamera import PiCamera

camera = PiCamera()

camera.resolution = (1280, 1024)



camera.capture('camera.png', format='png')


i had to modify the 'screenshot' on line 12 to be 'screenshot.png'.

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

Otherwise in the Spark Mobile app, while the image would display in the message feed, if you tapped to open the image it would try and load then give a 'File Type Not Supported' message. Which I could see the file it was trying to load was 'screenshot.'  (not the dot with no file extension)

So now its all working.


Dirk-Jan Uittenbogaard
Cisco Employee


Currently we don't support attaching more than 1 file using the API.

Please report this via ! Think this has to be fixed.

Content for Community-Ad

This widget could not be displayed.