Skip to content

Add support for uploading binary files for multipart/form-data bodies in python-http.client.#382

Merged
umeshp7 merged 6 commits intodevelopfrom
issue-366
Oct 20, 2020
Merged

Add support for uploading binary files for multipart/form-data bodies in python-http.client.#382
umeshp7 merged 6 commits intodevelopfrom
issue-366

Conversation

@webholik
Copy link
Contributor

Fixes #366.

Before:

import http.client
import mimetypes
conn = http.client.HTTPSConnection("postman-echo.com")
dataList = []
boundary = 'wL36Yn8afVp8Ag7AmP8qZ0SA4n1v9T'
dataList.append('--' + boundary)
dataList.append('Content-Disposition: form-data; name=ankit; filename={0}'.format('ques.2.jpeg'))

fileType = mimetypes.guess_type('test.jpeg')[0] or 'application/octet-stream'
dataList.append('Content-Type: {}'.format(fileType))
dataList.append('')

with open('test.jpeg') as f:
  dataList.append(f.read())
dataList.append('--'+boundary+'--')
dataList.append('')
body = '\r\n'.join(dataList)
payload = body
headers = {
   'Content-type': 'multipart/form-data; boundary={}'.format(boundary) 
}
conn.request("POST", "/post", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

There were two problems here - first that we were opening file in text mode instead of binary mode and the other that join method failed if the contents of dataList array were not string.

To resolve these issues, I have encoded each string in utf8 and opened the files in binary mode.
After:

import http.client
import mimetypes
from codecs import encode

conn = http.client.HTTPSConnection("postman-echo.com")
dataList = []
boundary = 'wL36Yn8afVp8Ag7AmP8qZ0SA4n1v9T'
dataList.append(encode('--' + boundary))
dataList.append(encode('Content-Disposition: form-data; name=ankit; filename={0}'.format('ques.2.jpeg')))

fileType = mimetypes.guess_type('test.jpeg')[0] or 'application/octet-stream'
dataList.append(encode('Content-Type: {}'.format(fileType)))
dataList.append(encode(''))

with open('test.jpeg', 'rb') as f:
  dataList.append(f.read())
dataList.append(encode('--'+boundary+'--'))
dataList.append(encode(''))
body = b'\r\n'.join(dataList)
payload = body
headers = {
   'Content-type': 'multipart/form-data; boundary={}'.format(boundary)
}
conn.request("POST", "/post", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

@webholik webholik requested a review from umeshp7 October 19, 2020 14:44
@umeshp7 umeshp7 merged commit 2482ec7 into develop Oct 20, 2020
@umeshp7 umeshp7 deleted the issue-366 branch December 15, 2020 09:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python-http.client codegens fails to run if non-text files are being uploaded in the request.

2 participants