50

I'm currently writing an unit test to check if http/2 is supported.

Is there a curl one-liner which checks if http/2 is supported and outputs a response that is easy to parse?

7 Answers 7

96

HTTP/2 supported:

$ curl -sI https://curl.se -o/dev/null -w '%{http_version}\n'
2

HTTP/2 not supported (instead serving 1.1 in this case):

$ curl -sI http://curl.se -o/dev/null -w '%{http_version}\n'
1.1

(curl 7.50.0 or later is required for this command line to work)

HTTP/3

Since curl 7.88.1, if you build curl to support HTTP/3, the above one-liner can be extended to also check for HTTP/3 support like this:

$ curl -sI --http3 https://curl.se -o/dev/null -w '%{http_version}\n'
3
2
  • 1
    It is giving me the following error: curl: unknown --write-out variable: 'http_version'
    – Hammad Dar
    Commented May 15, 2019 at 11:08
  • 4
    Run with -k (curl -ksI ...) if you're testing a server with self-signed certs.
    – David
    Commented Jul 15, 2020 at 13:02
11

Run

curl --version

and look for HTTP2 on the Features list

3
  • 5
    One-liner for this: curl --version | grep -i http2
    – Stalinko
    Commented Jun 18, 2020 at 11:25
  • 6
    The question is about http2 support for specific websites, not for curl itself!
    – hammady
    Commented Jul 28, 2020 at 8:10
  • The ask is here to check whether the website supports HTTP/2 or not. But not to check whether curl supports HTTP/2. Commented Oct 12, 2023 at 14:05
6

Here you can find a list of Tools for debugging, testing and using HTTP/2.

Probably the easiest one from the command line is:

$ is-http2 www.cloudflare.com

But that requires npm install -g is-http2-cli

For testing using curl you may need to compile it using the nghttp library, in macOS this can be done by using brew you could use:

$ brew install curl --with-nghttp2

And then you could use what @daniel-stenberg suggests in his answer

$ curl -sI https://curl.haxx.se -o/dev/null -w '%{http_version}\n'

In where you will get a 2 if http2 is supported.

2
  • 1
    Thank you for your answer. Your answer also contains options using other tools, which is very useful. However since Daniel Stenberg's answer is more fitting to the original question, I'm going to accept his answer.
    – bn4t
    Commented Jul 11, 2018 at 11:11
  • 1
    You know, @nbari is-http2 and curl output is different: curl -sI curl.haxx.se HTTP/2 200 server: Apache ... is-http2 curl.haxx.se:443 × HTTP/2 not supported by curl.haxx.se:443 Commented Apr 20, 2020 at 12:20
4

I used

curl -kvso /dev/null --http2 https://app.domain.com:443

which returned

...
> GET / HTTP/2
...
< HTTP/2 302
...

this is not checking if HTTP2 is supported, but check if HTTP2 actually works.

1
  • 5
    -kvso /dev/null is short form to more verbose option list: --insecure --verbose --silent --output /dev/null - I suggest to consult curl.haxx.se/docs/manpage.html and Ctrl+F those options there to see details. Commented Sep 7, 2020 at 6:52
2

You could simply do:

curl --http2 --head domain.com
1
  • 1
    Welcome to Stack Overflow! Please read How to Answer and edit your question to contain an explanation as to why this code would actually solve the problem at hand. Always remember that you're not only solving the problem, but are also educating the OP and any future readers of this post.
    – Adriaan
    Commented Jul 19, 2022 at 7:29
0

Accepted answer doesn't work for h2c in the example I'm looking at. Instead use:

curl --http2 -sI https://curl.se -o/dev/null -w '%{http_version}\n'

0

In mac, we can check the curl supported HTTP versions using the below command

curl --manual | grep "\-\-http" -A 10

--http0.9 - Tells curl to be fine with HTTP version 0.9 response

--http1.0 - Tells curl to use HTTP version 1.0 instead of using its internally preferred HTTP version.

--http1.1 - Tells curl to use HTTP version 1.1.

--http2 - Tells curl to use HTTP version 2.

--http3 - Tells curl to try HTTP/3 to the host in the URL, but fallback to earlier HTTP versions if the HTTP/3 connection establishment fails. HTTP/3 is only available for HTTPS and not for HTTP URLs.

$ curl -sI --http1.0 https://example.com -o/dev/null -w '%{http_version}\n'
1
$ curl -sI --http1.1 https://example.com -o/dev/null -w '%{http_version}\n'
1.1
$ curl -sI --http2 https://example.com -o/dev/null -w '%{http_version}\n'
2

Since example.com is configured to support HTTP\1.0, HTTP\1.1 and HTTP\2, it responds with corresponding HTTP version based on the incoming request from curl.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.