Blog

03/06/2019

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



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 https://google.com
            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 https://nixcraft.com/
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:

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 https://bash.sxi.io/
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: SXI ADMIN

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.

20/08/2019

Start your Bitcoin Exchange with our Software

Start your Bitcoin Exchange with our Software Setup your white label bitcoin exchange right away. Or create your own cryptocurrency using...
14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....