I am parsing .msg files and creating incidents thru the API and python requests module. I would like to add attachments to the created incidents if possible. I followed this post https://thwack.solarwinds.com/product-forums/solarwinds-service-desk-swsd/f/forum/92193/service-desk-api---add-attachment-to-new-incident/292997#292997 and was able to get a test to post with PowerShell. However, I cannot get attachments to work with Python requests.
Anyone know if it is possible to add attachments with requests? Here are some snippets of the code I am using/ have tried.
url = "https://api.samanage.com/attachments.json"
jsontoken = "Bearer ..."
headers = {'X-Samanage-Authorization': jsontoken,'Content-Type': 'multipart/form-data', 'Accept': 'application/vnd.samanage.v1.3+xml'}
incidentID = "1" #redacted for post here
attachment = {
"file[attachable_type]":"Incident",
"file[attachable_id]":incidentID
}
file = {
"file[attachment]":open("/Path/to/File", "r")
}
##file = {
## "file[attachment]": open("FILENAME.EXT","/PATH/TO/FILE", "rb")
response = requests.post(url, headers=headers, data=attachment, files=file)
print(response.status_code)
If I try to include the file in the attachments array, I get a TypeError: Object of type TextIOWrapper is not JSON serializable. But in the PowerShell format all that is together in one variable, so I wonder if it's just incompatible. I'm far-far-far from a requests expert/API expert and any help would be very much appreciated! Thank you.