- install via source
- get
- post
- put
- delete
- authorization
- content type
- others
- convert
- 12 Essential Curl Commands for Linux, Windows and macOS
- references
references:
available %-symbols:
content_type
curl_version
errormsg
exitcode
filename_effective
ftp_entry_path
http_code
- The numerical response code that was found in the last retrieved HTTP(S) or FTP(s) transferhttp_connect
http_version
json
local_ip
local_port
method
num_connects
num_headers
num_redirects
proxy_ssl_verify_result
redirect_url
referer
remote_ip
remote_port
response_code
- The numerical response code that was found in the last transfer (formerly known ashttp_code
)scheme
size_download
size_header
size_request
size_upload
speed_download
speed_upload
ssl_verify_result
stderr
stdout
time_appconnect
time_connect
time_namelookup
time_pretransfer
time_redirect
time_starttransfer
time_total
url
url_effective
urlnum
get via
$ curl -sSLg \ -k \ -o /dev/null \ -w "%{json}" \ https://domain.name.com | jq -r 'keys[]' | sort
install via source
[!NOTE|label:references:]
environment
[!TIP]
- centos:
gcc
:appstream
libssh2
:@epel
libssh2-devel
:@epel
:$ dnf install libssh2 libssh2-devel libssh2-docs
libssh2-doc
:@epel
zlib
:@centos-baseos
brotli
:@centos-baseos
:$ sudo dnf install brotli-devel.i686
libpsl
:@centos-baseos
openssl
:@centos-baseos
libidn2
:@centos-baseos
$ sudo dnf install -y autoconf automake libtool $ sudo dnf install -y wget gcc openssl-devel libssh2 libssh2-devel libssh2-docs brotli brotli-devel # nice to have $ sudo dnf install -y zstd libzstd libzstd-devel $ curl -fsSLgk -O https://github.com/curl/curl/releases/download/curl-8_2_1/curl-8.2.1.tar.gz $ tar xzf curl-8.2.1.tar.gz $ cd curl-8.2.1
- centos:
build
[!NOTE|label:references]
--prefix=/usr/local
will install in :/usr/local/lib
/usr/local/bin
/usr/local/include
/usr/local/share
$ ./configure --with-libssh \ --with-libssh2 \ --with-ssl \ --with-wolfssh \ --with-gssapi \ --enable-websockets \ --prefix=/opt/curl \ ... configure: Configured to build curl/libcurl: Host setup: x86_64-pc-linux-gnu Install prefix: /opt/curl Compiler: gcc CFLAGS: -Werror-implicit-function-declaration -O2 -Wno-system-headers -pthread CPPFLAGS: LDFLAGS: LIBS: -lssh2 -lssh2 -lssl -lcrypto -lssl -lcrypto -lgssapi_krb5 -lzstd -lz curl version: 8.2.1 SSL: enabled (OpenSSL) SSH: enabled (libSSH2) zlib: enabled brotli: no (--with-brotli) zstd: enabled (libzstd) GSS-API: enabled (MIT Kerberos/Heimdal) GSASL: no (libgsasl not found) TLS-SRP: enabled resolver: POSIX threaded IPv6: enabled Unix sockets: enabled IDN: no (--with-{libidn2,winidn}) Build libcurl: Shared=yes, Static=yes Built-in manual: enabled --libcurl option: enabled (--disable-libcurl-option) Verbose errors: enabled (--disable-verbose) Code coverage: disabled SSPI: no (--enable-sspi) ca cert bundle: /etc/pki/tls/certs/ca-bundle.crt ca cert path: no ca fallback: no LDAP: no (--enable-ldap / --with-ldap-lib / --with-lber-lib) LDAPS: no (--enable-ldaps) RTSP: enabled RTMP: no (--with-librtmp) PSL: no (libpsl not found) Alt-svc: enabled (--disable-alt-svc) Headers API: enabled (--disable-headers-api) HSTS: enabled (--disable-hsts) HTTP1: enabled (internal) HTTP2: no (--with-nghttp2, --with-hyper) HTTP3: no (--with-ngtcp2 --with-nghttp3, --with-quiche, --with-msh3) ECH: no (--enable-ech) WebSockets: enabled Protocols: DICT FILE FTP FTPS GOPHER GOPHERS HTTP HTTPS IMAP IMAPS MQTT POP3 POP3S RTSP SCP SFTP SMB SMBS SMTP SMTPS TELNET TFTP WS WSS Features: AsynchDNS GSS-API HSTS HTTPS-proxy IPv6 Kerberos Largefile NTLM NTLM_WB SPNEGO SSL TLS-SRP UnixSockets alt-svc libz threadsafe zstd WARNING: Websockets enabled but marked EXPERIMENTAL. Use with caution! $ make -j $ sudo make install # check $ tree -L 2 /opt/curl /opt/curl ├── bin │ ├── curl │ └── curl-config ├── include │ └── curl ├── lib │ ├── libcurl.a │ ├── libcurl.la │ ├── libcurl.so -> libcurl.so.4.8.0 │ ├── libcurl.so.4 -> libcurl.so.4.8.0 │ ├── libcurl.so.4.8.0 │ └── pkgconfig └── share ├── aclocal └── man
full configure
$ ./configure --with-libssh \
--with-libssh2 \
--with-ssl \
--with-openssl \
--with-zstd \
--with-wolfssh \
--with-gnu-ld \
--with-gssapi \
--with-zlib \
--with-brotli \ # ../lib/.libs/libcurl.so: undefined reference to `BrotliDecoderCreateInstance'`
--with-quiche \
--with-nghttp3 \
--with-ngtcp2 \
--with-libidn2 \
--with-zsh-functions-dir
--enable-debug \
--enable-optimize \
--enable-warnings \
--enable-werror \
--enable-curldebug \
--enable-http \
--enable-ftp \
--enable-file \
--enable-ldaps \
--enable-proxy \
--enable-dict \
--enable-telnet \
--enable-tftp \
--enable-smb \
--enable-manual \
--enable-libcurl-option \
--enable-libgcc \
--enable-ipv6 \
--enable-openssl-auto-load-config \
--enable-verbose \
--enable-unix-sockets \
--enable-socketpair \
--enable-http-auth \
--enable-netrc \
--enable-dnsshuffle \
--enable-websockets \
--enable-hsts \
--enable-headers-api \
--enable-alt-svc \
--enable-get-easy-options \
--prefix=/usr/local
Host setup: x86_64-pc-linux-gnu
Install prefix: /usr/local
Compiler: gcc
CFLAGS: -Werror-implicit-function-declaration -g -O2 -std=gnu89 -pedantic -Wall -W -Wpointer-arith -Wwrite-strings -Wunused -Wshadow -Winline -Wnested-externs -Wmissing-declarations -Wmissing-prototypes -Wno-long-long -Wbad-function-cast -Wfloat-equal -Wno-multichar -Wsign-compare -Wundef -Wno-format-nonliteral -Wendif-labels -Wstrict-prototypes -Wdeclaration-after-statement -Wold-style-definition -Wstrict-aliasing=3 -Wcast-align -Wtype-limits -Wold-style-declaration -Wmissing-parameter-type -Wempty-body -Wclobbered -Wignored-qualifiers -Wconversion -Wno-sign-conversion -Wvla -ftree-vrp -Wdouble-promotion -Wformat=2 -Warray-bounds=2 -Wshift-negative-value -Wshift-overflow=2 -Wnull-dereference -fdelete-null-pointer-checks -Wduplicated-cond -Wunused-const-variable -Wduplicated-branches -Wrestrict -Walloc-zero -Wformat-overflow=2 -Wformat-truncation=2 -Wimplicit-fallthrough=4 -Wno-system-headers -pthread
CPPFLAGS:
LDFLAGS: -L/usr/lib
LIBS: -lssh2 -lssh2 -lssl -lcrypto -lssl -lcrypto -lgssapi_krb5 -lldap -llber -lzstd -lzstd -lz -lgcc
curl version: 8.2.1
SSL: enabled (OpenSSL)
SSH: enabled (libSSH2)
zlib: enabled
brotli: enabled (libbrotlidec)
zstd: enabled (libzstd)
GSS-API: enabled (MIT Kerberos/Heimdal)
GSASL: no (libgsasl not found)
TLS-SRP: enabled
resolver: POSIX threaded
IPv6: enabled
Unix sockets: enabled
IDN: no (--with-{libidn2,winidn})
Build libcurl: Shared=yes, Static=yes
Built-in manual: enabled
--libcurl option: enabled (--disable-libcurl-option)
Verbose errors: enabled (--disable-verbose)
Code coverage: disabled
SSPI: no (--enable-sspi)
ca cert bundle: /etc/pki/tls/certs/ca-bundle.crt
ca cert path: no
ca fallback: no
LDAP: enabled (OpenLDAP)
LDAPS: enabled
RTSP: enabled
RTMP: no (--with-librtmp)
PSL: no (libpsl not found)
Alt-svc: enabled (--disable-alt-svc)
Headers API: enabled (--disable-headers-api)
HSTS: enabled (--disable-hsts)
HTTP1: enabled (internal)
HTTP2: no (--with-nghttp2, --with-hyper)
HTTP3: no (--with-ngtcp2 --with-nghttp3, --with-quiche, --with-msh3)
ECH: no (--enable-ech)
WebSockets: enabled
Protocols: DICT FILE FTP FTPS GOPHER GOPHERS HTTP HTTPS IMAP IMAPS LDAP LDAPS MQTT POP3 POP3S RTSP SCP SFTP SMB SMBS SMTP SMTPS TELNET TFTP WS WSS
Features: AsynchDNS GSS-API HSTS HTTPS-proxy IPv6 Kerberos Largefile NTLM NTLM_WB SPNEGO SSL TLS-SRP UnixSockets alt-svc brotli libz threadsafe zstd
WARNING: Websockets enabled but marked EXPERIMENTAL. Use with caution!
$ make -j && sudo make install
$ curl --version
WARNING: this libcurl is Debug-enabled, do not use in production
curl 8.2.1 (x86_64-pc-linux-gnu) libcurl/8.2.1 OpenSSL/1.1.1k-fips zlib/1.2.11 zstd/1.4.4 libssh2/1.9.0 OpenLDAP/2.4.46
Release-Date: 2023-07-26
Protocols: dict file ftp ftps gopher gophers http https imap imaps ldap ldaps mqtt pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp ws wss
Features: alt-svc AsynchDNS Debug GSS-API HSTS HTTPS-proxy IPv6 Kerberos Largefile libz NTLM NTLM_WB SPNEGO SSL threadsafe TLS-SRP TrackMemory UnixSockets zstd
$ curl-config --configure
'--without-brotli' '--with-quiche' '--with-zstd' '--with-nghttp3' '--with-ngtcp2' '--with-ssl' '--with-gssapi' '--enable-debug' '--enable-optimize' '--enable-warnings' '--enable-werror' '--enable-curldebug' '--enable-http' '--enable-ftp' '--enable-file' '--enable-ldaps' '--enable-proxy' '--enable-dict' '--enable-telnet' '--enable-tftp' '--enable-smb' '--enable-manual' '--enable-libcurl-option' '--enable-libgcc' '--enable-ipv6' '--enable-openssl-auto-load-config' '--enable-verbose' '--enable-unix-sockets' '--enable-socketpair' '--enable-http-auth' '--enable-netrc' '--enable-dnsshuffle' '--enable-websockets' '--enable-hsts' '--enable-headers-api' '--enable-alt-svc' '--enable-get-easy-options' '--with-openssl' '--with-gnu-ld' '--with-zlib' '--with-libssh2' '--with-libssh' '--with-wolfssh' '--with-libidn2' '--with-zsh-functions-dir' '--prefix=/usr/local'
for issue:
undefined reference to 'BrotliDecoderCreateInstance'
:$ make -j ../lib/.libs/libcurl.so: undefined reference to 'BrotliDecoderCreateInstance' ../lib/.libs/libcurl.so: undefined reference to 'BrotliDecoderVersion' ../lib/.libs/libcurl.so: undefined reference to 'BrotliDecoderDestroyInstance' ../lib/.libs/libcurl.so: undefined reference to 'BrotliDecoderDecompressStream' ../lib/.libs/libcurl.so: undefined reference to 'BrotliDecoderGetErrorCode' collect2: error: ld returned 1 exit status make[2]: *** [Makefile:1018: curl] Error 1
- solution:
$ ./configure --without-brotli ...
- solution:
set
[!NOTE|label:OPTIONAL] no need if using
/usr/local
as--prefix
$ sudo update-alternatives --install /usr/local/bin/curl curl /opt/curl/bin/curl 999 $ sudo update-alternatives --install /usr/local/bin/curl-config curl-config /opt/curl/bin/curl-config 999 # or /opt/curl $ bash -c "echo 'export LD_LIBRARY_PATH=/opt/curl/lib:$LD_LIBRARY_PATH' >> /etc/bashrc" $ bash -c "echo 'export LD_RUN_PATH=/opt/curl/lib:$LD_RUN_PATH' >> /etc/bashrc" # or /usr/local $ bash -c "echo 'export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH' >> /etc/bashrc" $ bash -c "echo 'export LD_RUN_PATH=/usr/local/lib:$LD_RUN_PATH' >> /etc/bashrc"
check
$ alternatives --list | grep curl curl auto /opt/curl/bin/curl curl-config auto /opt/curl/bin/curl-config $ curl --version curl 8.2.1 (x86_64-pc-linux-gnu) libcurl/8.2.1 OpenSSL/1.1.1k-fips zlib/1.2.11 zstd/1.4.4 libssh2/1.9.0 Release-Date: 2023-07-26 Protocols: dict file ftp ftps gopher gophers http https imap imaps mqtt pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp ws wss Features: alt-svc AsynchDNS GSS-API HSTS HTTPS-proxy IPv6 Kerberos Largefile libz NTLM NTLM_WB SPNEGO SSL threadsafe TLS-SRP UnixSockets zstd $ curl --help all
curl-config
[!NOTE|label:references:]
$ curl-config --libs
-L/usr/local/Cellar/curl/8.2.1/lib -lcurl
$ curl-config --feature
AsynchDNS
GSS-API
HSTS
HTTP2
HTTPS-proxy
IDN
IPv6
Kerberos
Largefile
MultiSSL
NTLM
NTLM_WB
SPNEGO
SSL
TLS-SRP
UnixSockets
alt-svc
brotli
libz
threadsafe
zstd
$ curl-config --cflags
-I/usr/local/Cellar/curl/8.2.1/include
$ curl-config --configure
'--disable-debug' '--disable-dependency-tracking' '--disable-silent-rules' '--prefix=/usr/local/Cellar/curl/8.2.1' '--with-ssl=/usr/local/opt/openssl@3' '--without-ca-bundle' '--without-ca-path' '--with-ca-fallback' '--with-secure-transport' '--with-default-ssl-backend=openssl' '--with-libidn2' '--with-librtmp' '--with-libssh2' '--without-libpsl' '--with-gssapi' 'CC=clang'
get
get JSON
$ curl https://reqbin.com/echo/get/json \
-H "Accept: application/json"
get XML
$ curl https://reqbin.com/echo/get/xml \
-H "Accept: application/xml"
get http_code
or response_code
$ curl -s -o /dev/null -w "%{http_code}" https://github.com
200
or
$ curl -s -o /dev/null -w "response: '%{response_code}'" https://github.com response: '200'
or
$ curl -sSgL -X GET https://github.com/fake/url | sed -nre 's!^.*"status"\s*:\s*([0-9]+).*$!\1!gp' 404
-
$ curl http://www.example.org -o >(cat >&1) -w "%{http_code}\n" 1>&2
get http_code
for multiple urls
$ xargs -n1 curl -sk -o /dev/null -w '%{http_code} ' < <(echo "https://1.domain.com https://2.domain.com")
# i.e.:
$ xargs -n1 curl -sk -o /dev/null -w '%{http_code} ' < <(echo "https://stackoverflow.com/questions/3110444/ https://stackoverflow.com/questions/3110444/")
301 301
or
$ echo "https://1.domain.com https://2.domain.com " > urls.txt $ xargs -n1 curl -sk -o /dev/null -w '%{http_code} ' < urls.txt
or
$ curl -sSgL -X GET https://1.domain.com https://2.domain.com | sed -nre 's!^.*"status"\s*:\s*([0-9]+).*$!\1!gp' 404 200
get size_download
$ curl -R -s -S -w "\nhttp: %{http_code}. size: %{size_download}\n" -o /dev/null https://github.com
get time
$ curl -s \
-w 'results: \n
Lookup time:\t%{time_namelookup}
Connect time:\t%{time_connect}
PreXfer time:\t%{time_pretransfer}
StartXfer time:\t%{time_starttransfer}
AppCon time:\t%{time_appconnect}
Redirect time:\t%{time_redirect}\n
Total time:\t%{time_total}\n' \
-o /deve/null \
https://github.com
results:
Lookup time: 0.001288
Connect time: 0.001617
PreXfer time: 0.080264
StartXfer time: 0.119895
AppCon time: 0.080165
Redirect time: 0.000000
Total time: 0.120600
post
post JSON data using Curl
$ curl -X POST https://reqbin.com/echo/post/json \
-H 'Content-Type: application/json' \
-d '{"login":"my_login","password":"my_password"}'
post a file using Curl
$ curl -d @data.json https://reqbin.com/echo/post/json
post form data using Curl
$ curl -X POST https://reqbin.com/echo/post/form \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "param1=value1¶m2=value2"
post XML
$ curl -X POST https://reqbin.com/echo/post/xml \
-H "Content-Type: application/xml" \
-H "Accept: application/xml" \
-d "<Request><Login>my_login</Login><Password>my_password</Password></Request>"
put
send PUT request
$ curl -X PUT https://reqbin.com/echo/put/json \
-d "PUT request data"
delete
syntax:
$ curl -X DELETE [URL] [options]
send a DELETE request
$ curl -X DELETE http://reqbin.com/sample/delete/json?id=1 \
-H "Accept: application/json"
authorization
Basic Auth Credentials
$ curl https://reqbin.com/echo \
-u "login:password"
Bearer Token Authorization Header
$ curl https://reqbin.com/echo/get/json \
-H "Accept: application/json" \
-H "Authorization: Bearer {token}"
Curl with a proxy
$ curl https://reqbin.com/echo \
-x myproxy.com:8080 \
-U login:password
content type
set the content type for a Curl request
$ curl -X POST https://reqbin.com/echo/post/json \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-d '{"Id": 78912, "Quantity": 1, "Price": 19.00}'
others
ssl
ignore invalid and self-signed SSL certificate errors in Curl
$ curl -k https://expired.badssl.com
make HTTPS requests with Curl
$ curl -k https://expired.badssl.com
with SSL connections
$ curl -k https://expired.badssl.com
send http header with curl request
$ curl https://reqbin.com/echo/get/json \
-H "X-Custom-Header: value" \
-H "Content-Type: application/json"
set a timeout
$ curl --connection-timeout 5 https://reqbin.com/echo
send a head request
$ curl -I https://reqbin.com/echo
send a OPTIONS request
$ curl https://api.reqbin.com/api/v1/requests \
-X OPTIONS \
-H "Access-Control-Request-Method: POST" \
-H "Access-Control-Request-Headers: content-type" \
-H "Origin: https://reqbin.com"
send a CORS request
$ curl -H "Origin: https://example.reqbin.com" \
https://reqbin.com/echo
send Cookies
$ curl --cookie "Name=Value" https://reqbin.com/echo
set the User-Agent string
$ curl https://reqbin.com/echo \
-A "ReqBin Curl Client/1.0"
watch web server returns
$ watch -n 15 curl -s --connect-timeout 10 http://www.google.com/
convert
convert to python requests
$ curl -X POST https://reqbin.com/echo/post/json \
-H "Content-Type: application/json" \
-d "{\"login\":\"my_login\",\"password\":\"my_password\"}"
convert to javascript/ajax calls
$ curl -X POST https://reqbin.com/echo/post/json \
-H "Content-Type: application/json" \
-d "{\"login\":\"my_login\",\"password\":\"my_password\"}"
convert to php code
$ curl -X POST https://reqbin.com/echo/post/json \
-H "Content-Type: application/json" \
-d "{\"login\":\"my_login\",\"password\":\"my_password\"}"
convert to http request
$ curl https://reqbin.com/echo/get/json \
-H "Content-Type: application/json" \
-H "Accept: application/json"
12 Essential Curl Commands for Linux, Windows and macOS
- get resource content by url
$ curl https://reqbin.com/echo
- save url content to a file
$ curl -o logo.png https://reqbin.com/static/img/logo.png
- download multiple files at once
$ curl -O https://reqbin.com/static/img/code/curl.png \ -O https://reqbin.com/static/img/code/java.png \ -O https://reqbin.com/static/img/code/python.png
- check page http headers
bash
- force curl to use http/2 protocol
$ curl --http2 https://reqbin.com
- do follow redirects
$ curl -L http://www.reqbin.com/echo
- use proxy server
$ curl -x proxy.domain.com:8080 -U user:password https://reqbin.com
- provide additional http headers with request
$ curl -H "Accept: application/json" https://reqbin.com/echo/get/json
- send data to the server
$ curl -d '{"id": 123456}' \ -H "Content-Type: application/json" \ https://reqbin.com/echo/post/json
- change the user-agent string
$ curl --user-agent "MyAppName 1.0" https://reqbin.com/echo
- send cookies to website
$ curl -b "name1=value1; name2=value2" https://reqbin.com
references
Top 20 Curl Flags
Flags | Description | Syntax |
---|---|---|
-O |
Download the file and save it under the original name | curl -O [URL] |
-o |
Download the file and save it with a different name | curl -o [file name] [URL] |
-X |
Specify the HTTP method to be used when sending the request | curl -X [method] [URL] |
-I or -head |
Print the title without the body of the document | curl -I [URL] |
-d |
Specify the data to send to the server | curl -d "key1=value1&key2=value2" [URL] |
-k or -insecure |
Ignore SSL Certificate Errors | curl -k [URL] |
-u or --user |
Specify the authentication data by passing a pair of login-password | curl -u [user:password] [URL] |
-F |
Submit form data as POST request | curl -F @field_name=@path/to/myFile |
--cookie |
Send HTTP cookies | curl --cookie "Name=Value" [URL] |
-x or --proxy |
Use a proxy server to upload files | curl -x "[protocol://][host][:port]" [URL] [options] |
--limit-rate |
Limit the download speed | curl --limit-rate [speed] -O [URL] |
-L or --location |
Follow Curl redirect using HTTP Location header | curl -L [URL] |
-v |
Makes Curl verbose | curl -v [URL] |
-m or --max-time |
Set a limit in seconds for the entire operation | curl -m [SECONDS] [URL] |
--connect-timeout |
Set a limit in seconds for a connection request | curl --connect-timeout [SECONDS] [URL] |
-T |
Transfers the specified local file to a remote URL | curl -T [file name] [URL] |
-H or --header |
Add additional HTTP request header | curl -H "X-Header: value" [URL] |
-D |
Save the HTTP headers that the site sends back | curl -D [URL] |
-A or --user-agent |
Set User-Agent string | curl -A "value" [URL] |
-C |
Resume an interrupted or intentionally stopped download | curl -C [OFFSET] -O [URL] |
write-out
Option | Description |
---|---|
Lookup time (time_namelookup ) |
The time, in seconds, it took from the start until the name resolving was completed |
Connect time (time_connect ) |
The time, in seconds, it took from the start until the TCP connect to the remote host was completed |
PreXfer time (time_pretransfer ) |
The time, in seconds, it took from the start until the file transfer was just about to begin. This includes all ‘pre-transfer’ commands and negotiations that are specific to the particular protocol(s) involved |
StartXfer time (time_starttransfer ) |
The time, in seconds, it took from the start until the first byte was just about to be transferred. This includes ‘time_pretransfer’ and also the time the server needed to calculate the result |
AppCon time (time_appconnect ) |
The time, in seconds, it took from the start until the SSL/SSH/etc connect/handshake to the remote host was completed (Added in 7.19.0) |
Redirect time (time_redirect ) |
The time, in seconds, it took for all redirection steps include name lookup, connect, pretransfer and transfer before the final transaction was started. ‘time_redirect’ shows the complete execution time for multiple redirections. (Added in 7.12.3) |