references:
available %-symbols:
http_code
- The numerical response code that was found in the last retrieved HTTP(S) or FTP(s) transfer
response_code
- The numerical response code that was found in the last transfer (formerly known as http_code
)
get via
Copy $ 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:
libssh2-devel
: @epel
: $ dnf install libssh2 libssh2-devel libssh2-docs
brotli
: @centos-baseos
: $ sudo dnf install brotli-devel.i686
Copy $ 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
build
[!NOTE|label:references]
--prefix=/usr/local
will install in :
Copy $ ./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
Copy $ ./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'
:
Copy $ 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:
Copy $ ./configure --without-brotli ...
set
[!NOTE|label:OPTIONAL] no need if using /usr/local
as --prefix
Copy $ 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
Copy $ 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
[!NOTE|label:references:]
Copy $ 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
Copy $ curl https://reqbin.com/echo/get/json \
-H "Accept: application/json"
Copy $ curl https://reqbin.com/echo/get/xml \
-H "Accept: application/xml"
Copy $ curl -s -o /dev/null -w "%{http_code}" https://github.com
200
or
Copy $ curl -s -o /dev/null -w "response: '%{response_code}'" https://github.com
response: '200'
or
Copy $ curl -sSgL -X GET https://github.com/fake/url | sed -nre 's!^.*"status"\s*:\s*([0-9]+).*$!\1!gp'
404
or
Copy $ curl http://www.example.org -o >( cat >&1 ) -w "%{http_code}\n" 1>&2
get http_code
for multiple urls
Copy $ 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
Copy $ echo "https://1.domain.com
https://2.domain.com
" > urls.txt
$ xargs -n1 curl -sk -o /dev/null -w '%{http_code} ' < urls.txt
or
Copy $ curl -sSgL -X GET https://1.domain.com https://2.domain.com | sed -nre 's!^.*"status"\s*:\s*([0-9]+).*$!\1!gp'
404
200
Copy $ curl -R -s -S -w "\nhttp: %{http_code}. size: %{size_download}\n" -o /dev/null https://github.com
Copy $ 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
Copy $ curl -X POST https://reqbin.com/echo/post/json \
-H 'Content-Type: application/json' \
-d '{"login":"my_login","password":"my_password"}'
Copy $ curl -d @data.json https://reqbin.com/echo/post/json
Copy $ curl -X POST https://reqbin.com/echo/post/form \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "param1=value1¶m2=value2"
Copy $ 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
Copy $ curl -X PUT https://reqbin.com/echo/put/json \
-d "PUT request data"
delete
syntax:
Copy $ curl -X DELETE [URL] [options]
Copy $ curl -X DELETE http://reqbin.com/sample/delete/json?id= 1 \
-H "Accept: application/json"
authorization
Copy $ curl https://reqbin.com/echo \
-u "login:password"
Copy $ curl https://reqbin.com/echo/get/json \
-H "Accept: application/json" \
-H "Authorization: Bearer {token}"
Copy $ curl https://reqbin.com/echo \
-x myproxy.com:8080 \
-U login:password
content type
Copy $ 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
Copy $ curl -k https://expired.badssl.com
make HTTPS requests with Curl
Copy $ curl -k https://expired.badssl.com
with SSL connections
Copy $ curl -k https://expired.badssl.com
Copy $ curl https://reqbin.com/echo/get/json \
-H "X-Custom-Header: value" \
-H "Content-Type: application/json"
Copy $ curl --connection-timeout 5 https://reqbin.com/echo
Copy $ curl -I https://reqbin.com/echo
Copy $ 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"
Copy $ curl -H "Origin: https://example.reqbin.com" \
https://reqbin.com/echo
Copy $ curl --cookie "Name=Value" https://reqbin.com/echo
Copy $ curl https://reqbin.com/echo \
-A "ReqBin Curl Client/1.0"
Copy $ watch -n 15 curl -s --connect-timeout 10 http://www.google.com/
convert
Copy $ curl -X POST https://reqbin.com/echo/post/json \
-H "Content-Type: application/json" \
-d "{\"login\":\"my_login\",\"password\":\"my_password\"}"
Copy $ curl -X POST https://reqbin.com/echo/post/json \
-H "Content-Type: application/json" \
-d "{\"login\":\"my_login\",\"password\":\"my_password\"}"
Copy $ curl -X POST https://reqbin.com/echo/post/json \
-H "Content-Type: application/json" \
-d "{\"login\":\"my_login\",\"password\":\"my_password\"}"
Copy $ curl https://reqbin.com/echo/get/json \
-H "Content-Type: application/json" \
-H "Accept: application/json"
get resource content by url
Copy $ curl https://reqbin.com/echo
save url content to a file
Copy $ curl -o logo.png https://reqbin.com/static/img/logo.png
download multiple files at once
Copy $ 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
force curl to use http/2 protocol
Copy $ curl --http2 https://reqbin.com
do follow redirects
Copy $ curl -L http://www.reqbin.com/echo
use proxy server
Copy $ curl -x proxy.domain.com:8080 -U user:password https://reqbin.com
provide additional http headers with request
Copy $ curl -H "Accept: application/json" https://reqbin.com/echo/get/json
send data to the server
Copy $ curl -d '{"id": 123456}' \
-H "Content-Type: application/json" \
https://reqbin.com/echo/post/json
change the user-agent string
Copy $ curl --user-agent "MyAppName 1.0" https://reqbin.com/echo
send cookies to website
Copy $ curl -b "name1=value1; name2=value2" https://reqbin.com
references
Flags Description Syntax Download the file and save it under the original name
Download the file and save it with a different name
curl -o [file name] [URL]
Specify the HTTP method to be used when sending the request
Print the title without the body of the document
Specify the data to send to the server
curl -d "key1=value1&key2=value2" [URL]
Ignore SSL Certificate Errors
Specify the authentication data by passing a pair of login-password
curl -u [user:password] [URL]
Submit form data as POST request
curl -F @field_name=@path/to/myFile
curl --cookie "Name=Value" [URL]
Use a proxy server to upload files
curl -x "[protocol://][host][:port]" [URL] [options]
curl --limit-rate [speed] -O [URL]
Follow Curl redirect using HTTP Location header
Set a limit in seconds for the entire operation
Set a limit in seconds for a connection request
curl --connect-timeout [SECONDS] [URL]
Transfers the specified local file to a remote URL
curl -T [file name] [URL]
Add additional HTTP request header
curl -H "X-Header: value" [URL]
Save the HTTP headers that the site sends back
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)