Smooch Upload Attachments endpoint is not working in Python | Community
Skip to main content

Smooch Upload Attachments endpoint is not working in Python

  • April 1, 2022
  • 0 replies
  • 0 views

Murali

Hey, so I'm trying to upload an attachment using this https://docs.smooch.io/rest/#operation/uploadAttachment endpoint. It says that it takes a multipart form data as the payload where the file is sent as a "binary <string>" with the name "source". But I tried to send the file's stream as "source" in python, I get an error message saying "No source file specified". 

Now when I try the same thing with postman or insomnia, it works fine. In postman/insomnia, I'm uploading a file from my local machine whereas, in python, the file is in a FileStorage format. When I do file.read(), I get the binary data. I'm not exactly sure how this is supposed to work. Here is my code sample

url = f"https://api.smooch.io/v2/apps/{self.app_id}/attachments?access=public"

headers = {
"Authorization": "Bearer {}".format(self.jwt)
}

response = requests.request(
"POST",
url,
headers=headers,
files={"source": file.read()},
)
return response.json()

 

Let me also show you a working example from a postman. This is what I get when I generate the code. 

url = "https://api.smooch.io/v2/apps/<....>/attachments?access=public"

files=[('source',('79272208.png',open('/Users/muralikomaravolu/Downloads/79272208.png','rb'),'image/png'))]
headers = {
'Authorization': 'Bearer .....'
}
response = requests.request("POST", url, headers=headers, data={}, files=files)
In insomnia, the file gets sent under 'data' instead of 'files' but that's a different problem entirely. Would love any kind of help. 
Thank you!