Skip to content

Shorten output where possible#38

Open
dlenski wants to merge 5 commits into
ofw:masterfrom
dlenski:shorten
Open

Shorten output where possible#38
dlenski wants to merge 5 commits into
ofw:masterfrom
dlenski:shorten

Conversation

@dlenski

@dlenski dlenski commented May 5, 2026

Copy link
Copy Markdown

This PR updates the module to use shorter curl command-line options where they are available and semantically equivalent. All tests are updated to pass, and an optional modern=False option can be specified to prevent it from using the --json option added in curl 7.82.

  • Document 'pretty' parameter

  • Use abbreviated options for 'User-Agent' and 'Cookie' headers

    The '-A' and '-b' options for these specific headers have been
    available in curl for a very long time.

  • Use '--json', and omit 'Content-Length' when set automatically

    The '--json' option was added in curl 7.82, and it implies both
    'Content-Type: application/json' and 'Accept: application/json'.

    https://daniel.haxx.se/blog/2022/02/02/curl-dash-dash-json/

dlenski added 3 commits May 5, 2026 13:24
The '-A' and '-b' options for these specific headers have been
available in curl for a very long time.
The '--json' option was added in curl 7.82, and it implies *both*
'Content-Type: application/json' and 'Accept: application/json'.

https://daniel.haxx.se/blog/2022/02/02/curl-dash-dash-json/
dlenski added 2 commits May 5, 2026 14:51
Curl defaults to 'Content-Type: application/x-www-form-urlencoded' when
-d/--data-raw are specified, so there's no need to repeat this.
This is curl's default behavior with HTTP/1.1, and does nothing with HTTP/2
and newer protocol versions.

The other plausible value of this header is 'Connection: close', and should
still be provided to curl.
Comment thread curlify.py
Comment on lines +33 to +35
if isinstance(body, bytes):
body = body.decode('utf-8')
inferred_length = len(body)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what the reasonable behavior here is for a non-ASCII string.

On systems with UTF-8 shells, curl will use the UTF-8 string provided by the shell. For example, this will return 7, because the length of the string Hééé encoded as UTF-8 is 7 bytes, and so curl fills in Content-Length: 7 for this payload:

curl -s https://httpbin.org/post -d 'Hééé' | jq '.headers["Content-Length"] | tonumber'

But on other systems, the encoding will be different.

If body is bytes or a pure-ASCII string, inferred_length = len(body) is reasonable.

But if body is any other string, the behavior of Python requests (defaults to UTF-8) and curl may differ.

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.

1 participant