Reputation: 353
I'm trying to upload a PDF as an attachment to a Trello card using python-requests
. I've been unable to get the request in the function below to return anything other than 400: Error parsing body
despite significant tweaks (detailed below).
I should note that I'm able to create cards and add URL attachments to them (neither of which require a file upload) without any problems.
Here's the code that handles the POST of the file:
def post_pdf(session, design, card_id):
attachment = {
"name": design["campaign_title"] + " - Combined PDF",
"mimeType": "application/pdf"
}
pdf_post = session.post(
url = "https://api.trello.com/1/cards/" + card_id + "/attachments",
files = {"file": open("combined_pdf.pdf", "rb")},
data = attachment
)
The authentication key and token are set Session params when the session was created, so they're not added here.
Also, in the actual code, the POST
is handled by a wrapper function that adds some boilerplate error-checking and rate limiting to the request, as well as more-verbose error dumps when a request fails, but I've confirmed (in the above example) that the same error persists without the wrapper.
Adjustments I've tried
data = attachment
with json = attachment
data = attachment
with params = attachment
attachment
completely and POST
ing the file with no associated datastream = True
to the request parameters (this doesn't seem to matter for uploads, but I figured it couldn't hurt to try)base64
(this encoding has been required elsewhere; I was grasping at straws)base64
, combined with the above tweaks to data
/ json
/ params
Note: The PDF file is potentially a source of the problem - it's generated by converting several images to PDF format and then concatenating them with pdfunite
, so I could well have made mistakes in its creation that are causing Trello to reject the file. What seems to confirm this is that Googling for Trello "Error parsing body"
returns two hits, only one of which deals with Trello, and neither of which are useful. This leads me to think that this is a particularly odd / rare error message, which means to me that I've made some kind of serious error encoding the file.
However, the PDF file opens properly on my (and my coworkers') systems without any error messages, artifacts, or other strange behavior. More importantly, trying this with other "known good" PDFs also fails, with the same error code. Because the file's contents fall within the bounds of "company property / information", I'd like to avoid posting it (and / or the raw request body), but I'll do so if there's agreement that it's causing the problem.
Upvotes: 2
Views: 3338
Reputation: 353
I found the solution: the Content-Type
header was set incorrectly due to a session-wide setting ( Session.headers.update({"Content-Type": "application/json"})
) overriding the multipart/form-data
header when the upload request was sent. This caused Trello to reject the body. I solved the problem by removing the session-level header, which allowed requests
to modify the content type for each request.
Upvotes: 3