How to use curl command with http/2 on MacOS X

in Categories , , , , last updated November 1, 2016

I am using a MacOS sierra (version 10.12.1). How do I use curl command with http2/ protocol? I am getting an error that read as follows:

            curl -I --http2
            curl: (1) Unsupported protocol

How do I solve this problem and use cURL with HTTP/2 on Mac OS X or MacOS Sierra?

The default version of curl does not support HTTP/2. Here is my current version on MacOS sierra:
$ curl --version
curl 7.49.1 (x86_64-apple-darwin16.0) libcurl/7.49.1 SecureTransport zlib/1.2.8
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz UnixSockets

So if I try to pass the --http2 option to curl command, I will get the following error on screen:
$ curl -I --http2
curl: (1) Unsupported protocol

Fixing curl: (1) Unsupported protocol error on MacOS

Use the brew command as follows to install curl with nghttp2. nghttp2 is an implementation of HTTP/2 and its header compression algorithm HPACK in C. You need to compile and install cURL as follows:
$ brew update
$ brew upgrade
$ brew install curl --with-nghttp2

Sample outputs:

Fig.01: Fixing curl: (1) Unsupported protocol on Mac OS X
Fixing curl: (1) Unsupported protocol on Mac OS X

Please note down the path “/usr/local/Cellar/curl/7.50.3/bin/curl”

Create a shortlink in your ~/bin/

Type the following command:
$ mkdir $HOME/bin/
$ cd ~/bin/
$ ln -s /usr/local/Cellar/curl/7.50.3/bin/curl
$ ~/bin/curl --version

Sample outputs:

curl 7.50.3 (x86_64-apple-darwin16.1.0) libcurl/7.50.3 OpenSSL/1.0.2j zlib/1.2.8 nghttp2/1.16.0
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp 
Features: IPv6 Largefile NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets 

How do I test it?

Pass the --http2 flag to http2 enabled site:
$ ~/bin/curl -I --http2
Sample outputs:

HTTP/2 200 
server: nginx
date: Tue, 01 Nov 2016 21:59:18 GMT
content-type: text/html;charset=utf-8
vary: Accept-Encoding
cache-control: public,max-age=900,s-maxage=900,must-revalidate
expires: Tue, 01 Nov 2016 22:03:40 GMT
etag: de8af9a212f436d9d4d45f4cc994b2ca
last-modified: Mon, 05 Sep 2016 07:49:45 GMT
front-end-https: on
strict-transport-security: max-age=15768000
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-xss-protection: 1

A note about replacing the MacOS curl with updated version

You can create a softlink to the formula to replace the system cURL with PATH magic,
$ brew link curl --force
$ curl --version

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Share this on (or read 1 comments/add one below):

1 comment

  1. The macOS Sierra 10.12.1 curl has some additional features that you’ll lose if you build –with-nghttp2. The missing ones are: AsynchDNS, GSS-API, Kerberos, and SPNEGO (as listed in the ‘curl –version’ output). To get HTTP/2 and the missing features too, use:

    $ brew install curl --with-nghttp2 --with-gssapi --with-c-ares

    Have a question? Post it on our forum!