Fixing a `form-data` boundary error
I'm starting to use err
tag on this blog to document these small things.
I was trying out an endpoint that takes a file field. The code looked something like this.
1import requests
2
3file = {'document': open('document.pdf', 'rb')}
4
5headers = {
6 'Content-Type': 'multipart/form-data',
7 'Accept': 'application/json'
8}
9
10response = requests.post(url,
11 files=file,
12 headers=headers)
Upon sending this request, I was greeted with a "boundary error".
The reason why this is happening is requests
will try to write the Content-Type
and boundary strings in the post request. If you manually set Content-Type
, the boundary strings will be missing.