Repository navigation
Closed
Labels
requests-compatIssues related to Requests backwards compatibilityIssues related to Requests backwards compatibility
Description
Hi. The following works with Requests, but not HTTPX. Can you please help me understand how to make it work:
import requests
import uuid
import subprocess
import json
import os
import httpx
newName = uuid.uuid4().hex
event = uuid.uuid4().hex
subprocess.call("cp index.jpeg /tmp/%s.jpeg"%(newName),shell=True)
url = 'http://myserver.us-west-2.compute.amazonaws.com'
event = "ASDGASFDASDF"
payload = {'event': "%s"%(event) , 'confidence': 95}
thermalName = "/tmp/%s.jpeg"%(newName)
newName = thermalName
files = {
'json': (None, json.dumps(payload), 'application/json'),
'image': (os.path.basename(newName), open(newName, 'rb'), 'application/octet-stream'),
'image1': (os.path.basename(newName), open(newName, 'rb'), 'application/octet-stream'),
'image2': (os.path.basename(newName), open(newName, 'rb'), 'application/octet-stream')
}
# Do the HTTP POST
#r = requests.post(url, files=files)
r = httpx.post(url, files=files)
print(r.text)
The error I get is:
Traceback (most recent call last):
File "test-post.py", line 32, in <module>
r = httpx.post(url, files=files)
File "/usr/local/lib/python3.6/dist-packages/httpx/api.py", line 183, in post
trust_env=trust_env,
File "/usr/local/lib/python3.6/dist-packages/httpx/api.py", line 53, in request
trust_env=trust_env,
File "/usr/local/lib/python3.6/dist-packages/httpx/client.py", line 718, in request
cookies=cookies,
File "/usr/local/lib/python3.6/dist-packages/httpx/client.py", line 336, in build_request
cookies=cookies,
File "/usr/local/lib/python3.6/dist-packages/httpx/models.py", line 696, in __init__
content, content_type = self.encode_data(data, files, json)
File "/usr/local/lib/python3.6/dist-packages/httpx/models.py", line 619, in encode_data
content, content_type = multipart_encode(data or {}, files)
File "/usr/local/lib/python3.6/dist-packages/httpx/multipart.py", line 103, in multipart_encode
body.write(field.render_headers())
File "/usr/local/lib/python3.6/dist-packages/httpx/multipart.py", line 66, in render_headers
filename = _format_param("filename", self.filename)
File "/usr/local/lib/python3.6/dist-packages/httpx/multipart.py", line 121, in _format_param
value = _HTML5_FORM_ENCODING_RE.sub(replacer, value)
TypeError: expected string or bytes-like object
Metadata
Metadata
Assignees
Labels
requests-compatIssues related to Requests backwards compatibilityIssues related to Requests backwards compatibility
Activity
sethmlarson commentedon Oct 17, 2019
Thanks for reporting this! Our multipart support definitely isn't exercised enough in the test suite.
yeraydiazdiaz commentedon Oct 17, 2019
I had a quick look at this and considering our docs on multipart encoding:
filesas aDict[str, tuple]the first element of the tuple must be a string. In your example you're setting it toNonewhich is causing the error.strwhich also causes an error.I can confirm both of those are accepted in requests.