BudiBadu Logo
Samplebadu

cURL by Example: HTTP/2 Request

Latest

Force the use of HTTP/2. Modern cURL versions try to negotiate this automatically.

Code

# Force HTTP/2
curl --http2 https://example.com

# Force HTTP/2 without TLS (h2c) - rare
curl --http2-prior-knowledge http://localhost:8080

Explanation

HTTP/2 brings several performance improvements over HTTP/1.1. Modern browsers and servers support it automatically when using HTTPS.

Main advantages of HTTP/2:

  • Multiplexing: Multiple requests share a single connection
  • Header compression: Reduces overhead for repeated headers
  • Server push: Servers can send resources before being requested
  • Binary protocol: More efficient than text-based HTTP/1.1

The --http2 flag tells cURL to prefer HTTP/2 if available. The server must support it and the connection usually requires TLS. The protocol negotiation happens through ALPN (Application-Layer Protocol Negotiation) during the TLS handshake.

HTTP/2 over cleartext (h2c) is rare but useful for local testing. The --http2-prior-knowledge flag skips the upgrade handshake and assumes the server supports HTTP/2. This only works with servers explicitly configured for h2c.

Your cURL version must be compiled with nghttp2 support. Run curl -V and look for HTTP2 in the features list to verify support.

Code Breakdown

2
--http2 requests an upgrade to HTTP/2.
5
--http2-prior-knowledge skips the upgrade handshake (for non-TLS).
All
Requires cURL to be built with nghttp2 support.