I’m trying to upload documents to the document-control module via API using Python requests library, but getting a 400 error: “multipart boundary not found in Content-Type header”.
Here’s my upload code:
files = {'file': open('SOP-001.pdf', 'rb')}
data = {'documentNumber': 'SOP-001', 'revision': 'A'}
response = requests.post(api_url, files=files, data=data, headers=headers)
I’ve verified the endpoint URL is correct and authentication works fine for GET requests. The file exists and is readable. Is there a specific way Mastercontrol expects multipart form-data to be encoded?
The document-control API expects metadata as JSON in a specific field, not as separate form fields. You need to send metadata as a JSON string in the ‘metadata’ field of the multipart form. I ran into this exact issue last month.
Are you manually setting Content-Type in your headers? If so, remove it. The requests library automatically sets the correct multipart boundary when you use the files parameter, but manual headers override this.
Yes! I was setting Content-Type: application/json in my headers dict because other API calls need it. Removing that header fixed the boundary error. But now I’m getting a 422 error: “Invalid document metadata”. Do I need to structure the data parameter differently?
You need to send the JSON string as the value for a ‘metadata’ key in the data dict, not replace the entire data dict. Also make sure your JSON includes all required fields like documentType and owner.
Can you show an example? I tried wrapping my data dict in json.dumps() but still getting the 422 error.
Also, don’t forget to properly close the file handle. Using a context manager is cleaner and prevents resource leaks in long-running automation scripts.