Zapier Python Code: Sending multipart/form-data POST request with file attachment

I’m trying to automate some tasks using Zapier, but I’m stuck on sending a multipart/form-data POST request with a file attachment. I got it working in Postman with Debitoor’s API, but I can’t seem to replicate this in Zapier when using Python Code. I’m receiving an inbound email with an attachment, which Zapier converts into a hydrated file. The challenge is figuring out how to incorporate this hydrated file into my Python POST request. I’ve tried adapting the code from Postman but I’m unsure how to properly include the file data.

Here’s an alternative code snippet I’m considering:

import requests

endpoint = 'https://api.example.com/uploadFile'
headers = {'Content-Type': 'multipart/form-data'}
attachment = 'hydrate|||exampleHydratedData|||hydrate'

# How can I properly include the attachment in the POST request?
result = requests.post(endpoint, headers=headers)
print(result.text)

Any help on integrating the hydrated file into the Python request would be really appreciated!

I’ve encountered similar challenges with Zapier’s Python Code and multipart/form-data requests. One approach that worked for me was using the requests-toolbelt library. It provides a MultipartEncoder class that simplifies handling multipart form data.

Here’s a modified version of your code that might help:

from requests_toolbelt import MultipartEncoder
import requests

endpoint = 'https://api.example.com/uploadFile'
attachment = 'hydrate|||exampleHydratedData|||hydrate'

m = MultipartEncoder(
    fields={'file': ('filename.pdf', attachment, 'application/pdf')}
)

headers = {'Content-Type': m.content_type}
result = requests.post(endpoint, headers=headers, data=m)
print(result.text)

This method allows you to specify the filename, content, and MIME type for your attachment. It also automatically sets the correct Content-Type header. Remember to add requests-toolbelt to your Zapier Python environment if it’s not already available.

hey miar, i’ve dealt with this before. you need to use the files parameter in requests.post(). try something like:

files = {'file': ('filename.pdf', attachment, 'application/pdf')}
result = requests.post(endpoint, headers=headers, files=files)

this should work for sending the hydrated file. let me know if you need more help!

I’ve had success with a slightly different approach using the requests library. Instead of setting the Content-Type header manually, let the library handle it:

import requests

endpoint = 'https://api.example.com/uploadFile'
attachment = 'hydrate|||exampleHydratedData|||hydrate'

files = {'file': ('filename.pdf', attachment, 'application/pdf')}
data = {'key1': 'value1', 'key2': 'value2'}  # Add any additional form data here

result = requests.post(endpoint, files=files, data=data)
print(result.text)

This method allows you to include both the file and any additional form data in a single request. The requests library will automatically set the appropriate headers and handle the multipart encoding. Just make sure to replace ‘filename.pdf’ with your actual filename and adjust the MIME type if needed. This approach has worked well for me in similar Zapier Python Code scenarios.