3.0.4: Improved request-response example - #3997
Conversation
handrews
left a comment
There was a problem hiding this comment.
I'm pretty sure I'm missing something here with the content-length, but I'm not sure what?
| Host: example.org | ||
| Content-Type: application/json | ||
| Content-Length: 187 | ||
| Content-Length: 188 |
There was a problem hiding this comment.
I'm confused by both numbers- to me it looks like it's 260 if there is a trailing newline (more if the line ends are CRLF):
Python 3.8.13 (default, May 11 2022, 14:42:53)
[Clang 13.1.6 (clang-1316.0.21.2.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x = """{
... "failedUrl" : "https://clientdomain.com/failed",
... "successUrls" : [
... "failedUrl": "https://clientdomain.com/failed",
... "successUrls": [
... "https://clientdomain.com/fast",
... "https://clientdomain.com/medium",
... "https://clientdomain.com/slow"
... ]
... }
... """
>>> x
'{\n "failedUrl" : "https://clientdomain.com/failed",\n "successUrls" : [\n "failedUrl": "https://clientdomain.com/failed",\n "successUrls": [\n "https://clientdomain.com/fast",\n "https://clientdomain.com/medium",\n "https://clientdomain.com/slow"\n ]\n}\n'
>>> len(x)
260
>>> There was a problem hiding this comment.
Funny, I did something similar with Node.js and get 188:
Welcome to Node.js v22.5.1.
Type ".help" for more information.
> var text = `{
... "failedUrl": "https://clientdomain.com/failed",
... "successUrls": [
... "https://clientdomain.com/fast",
... "https://clientdomain.com/medium",
... "https://clientdomain.com/slow"
... ]
... }`
undefined
> text
'{\n' +
' "failedUrl": "https://clientdomain.com/failed",\n' +
' "successUrls": [\n' +
' "https://clientdomain.com/fast",\n' +
' "https://clientdomain.com/medium",\n' +
' "https://clientdomain.com/slow"\n' +
' ]\n' +
'}'
> text.length
188Which is identical to what my editor shows when I select that text block in the markdown source.
There was a problem hiding this comment.
Out of curiosity I repeated the experiment with Python:
Python 3.9.6 (default, Feb 3 2024, 15:58:27)
[Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> text="""{
... "failedUrl": "https://clientdomain.com/failed",
... "successUrls": [
... "https://clientdomain.com/fast",
... "https://clientdomain.com/medium",
... "https://clientdomain.com/slow"
... ]
... }"""
>>> text
'{\n "failedUrl": "https://clientdomain.com/failed",\n "successUrls": [\n "https://clientdomain.com/fast",\n "https://clientdomain.com/medium",\n "https://clientdomain.com/slow"\n ]\n}'
>>> len(text)
188Which produces the same length of 188 as Node.js.
There was a problem hiding this comment.
I reproduced Ralf's experiment with the same result, so 188 does seem to be the right length. But I think the result of len(text) in Python is in units of unicode characters, which may not be the length of a UTF-8-encoded response body (though in this case it is). The way to get the length of a string encoded to UTF-8 in Python is
>>> len(text.encode('utf-8'))
188There was a problem hiding this comment.
188 it is! I swear I stared at that and could not figure out what was going on.... sorry to take up your time, folks!

Fixes #1720