You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Originally posted by plotski June 21, 2021
I just spent several hours debugging LocalProtocolError("Too much data for declared Content-Length"). I've figured out what's going on, but I don't know what the best fix would be or if this is entirely my own fault.
I was passing a StringIO object to a POST request, e.g.:
>>>files= {'upload-file': ('foo', StringIO('cöntént of föö'))}
>>>r=httpx.post("https://httpbin.org/post", files=files)
The StringIO object ends up in FileField.get_length(), which calls peek_filelike_length(), which correctly reports the the length of the string. However, we must know the length of the string as bytes because that's what we need for the POST request.
The fix is very simple: Use BytesIO instead of StringIO. But the error message was extremely unhelpful.
Maybe FileField shouldn't accept a StringIO object? Or maybe it should convert to bytes first? Or maybe this is really obvious for anyone with more experience and there's no need to fix it?
I'm fairly sure that the most sensible thing to do here is to raise an error on:
Files opened in text mode.
StringIO instances, used instead of BytesIO instances.
The text was updated successfully, but these errors were encountered:
Based on this discussion... #1704
Originally posted by plotski June 21, 2021
I just spent several hours debugging
LocalProtocolError("Too much data for declared Content-Length")
. I've figured out what's going on, but I don't know what the best fix would be or if this is entirely my own fault.I was passing a
StringIO
object to a POST request, e.g.:The
StringIO
object ends up inFileField.get_length()
, which callspeek_filelike_length()
, which correctly reports the the length of the string. However, we must know the length of the string asbytes
because that's what we need for the POST request.The fix is very simple: Use
BytesIO
instead ofStringIO
. But the error message was extremely unhelpful.Maybe
FileField
shouldn't accept aStringIO
object? Or maybe it should convert tobytes
first? Or maybe this is really obvious for anyone with more experience and there's no need to fix it?I'm fairly sure that the most sensible thing to do here is to raise an error on:
StringIO
instances, used instead ofBytesIO
instances.The text was updated successfully, but these errors were encountered: