How To
How to upload files via RESTful API and link them to Attachments fields
while it’s possible to upload attachments to records via the swimlane python driver, it’s also possible to do it directly by using the api this guide will give a brief overview of how to use the restful api to upload files to a record and link them to an attachment field first, import the packages and connect to the restful api from swimlane import swimlane import json swimlane = swimlane('host', 'user', 'password', verify ssl=false) post the byte stream to the restful api file name = 'api pod logs txt' file path = r'c \user\swimlane\api pod logs txt' resp = swimlane request('post','/attachment', files={'file' (file name, open(file path, 'rb'))}, verify=false) resp raise for status() rdata = resp json()\[0] finally, link the byte stream’s identifier to the swimlane record resp = swimlane request('get', '/app/{}/record/{}' format('avgjpj3e43tkwtqy5','al khxqqzkd1iwyne')) resp raise for status() rec db12 1 = resp json() rec db12 1\['values']\['afihc'] append({'$type' 'core models record attachment, core', 'fileid'\ rdata\['fileid'], 'filename'\ file name, 'uploaddate'\ rdata\['uploaddate']}) resp = swimlane request('put', '/app/{}/record/{}' format('avgjpj3e43tkwtqy5','al khxqqzkd1iwyne'), json=rec db12 1) some additional notes we’re only using the swimlane driver to connect to the api the rest is all done with api calls when the file is uploaded, it’s stored in a segmented fashion by using mongodb’s gridfs feature the files are stored in two separate collections (fs files, and fs chunks) the full code from swimlane import swimlane import json \# connect to restful api swimlane = swimlane('host', 'user', 'password', verify ssl=false) \# post the byte stream to restful api file name = 'api pod logs txt' file path = r'\<filename example> txt' resp = swimlane request('post','/attachment', files={'file' (file name, open(file path, 'rb'))}, verify=false) resp raise for status() rdata = resp json()\[0] \# link the byte stream's identifier to the swimlane record resp = swimlane request('get', '/app/{}/record/{}' format('avgjpj3e43tkwtqy5','al khxqqzkd1iwyne')) resp raise for status() rec db12 1 = resp json() rec db12 1\['values']\['afihc'] append({'$type' 'core models record attachment, core', 'fileid'\ rdata\['fileid'], 'filename'\ file name, 'uploaddate'\ rdata\['uploaddate']}) resp = swimlane request('put', '/app/{}/record/{}' format('avgjpj3e43tkwtqy5','al khxqqzkd1iwyne'), json=rec db12 1)