cancel
Showing results for 
Search instead for 
Did you mean: 
cancel

Uploading Local Files to Spark

1831
Views
15
Helpful
3
Comments
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
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)]

Thanks

Tim

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.start_preview()

sleep(2)

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

camera.stop_preview()


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.

Riles

Cisco Employee

Timothy,

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

Please report this via devsupport@ciscospark.com ! Think this has to be fixed.

CreatePlease to create content
Content for Community-Ad
August's Community Spotlight Awards