Skip to content
Search

CURL:Examples

lastmod 2024-07-23created 2020-07-0911 edits

Examples using cURL.

Simple Usage

Get the main page from a web-server:

curl https://www.example.com/

Get a README file from an FTP server:

curl ftp://ftp.funet.fi/README

Get a web page from a server using port 8000:

curl http://www.weirdserver.com:8000/

Get a directory listing of an FTP site:

curl ftp://ftp.funet.fi

Get the all terms matching curl from a dictionary:

curl dict://dict.org/m:curl

Get the definition of curl from a dictionary:

curl dict://dict.org/d:curl

Fetch two documents at once:

curl ftp://ftp.funet.fi/ http://www.weirdserver.com:8000/

Get a file off an FTPS server:

curl ftps://files.are.secure.com/secrets.txt

or use the more appropriate FTPS way to get the same file:

curl --ftp-ssl ftp://files.are.secure.com/secrets.txt

Get a file from an SSH server using SFTP:

curl -u username sftp://example.com/etc/issue

Get a file from an SSH server using SCP using a private key (not password-protected) to authenticate:

curl -u username: --key ~/.ssh/id_rsa scp://example.com/~/file.txt

Get a file from an SSH server using SCP using a private key (password-protected) to authenticate:

curl -u username: --key ~/.ssh/id_rsa --pass private_key_password
scp://example.com/~/file.txt

Get the main page from an IPv6 web server:

curl "http://[2001:1890:1112:1::20]/"

Get a file from an SMB server:

curl -u "domain\username:passwd" smb://server.example.com/share/file.txt

Uploading

FTP / FTPS / SFTP / SCP

Upload all data on stdin to a specified server:

curl -T - ftp://ftp.upload.com/myfile

Upload data from a specified file, login with user and password:

curl -T uploadfile -u user:passwd ftp://ftp.upload.com/myfile

Upload a local file to the remote site, and use the local file name at the remote site too:

curl -T uploadfile -u user:passwd ftp://ftp.upload.com/

Upload a local file to get appended to the remote file:

curl -T localfile -a ftp://ftp.upload.com/remotefile

Curl also supports ftp upload through a proxy, but only if the proxy is configured to allow that kind of tunneling. If it does, you can run curl in a fashion similar to:

curl --proxytunnel -x proxy:port -T localfile ftp.upload.com

SMB / SMBS

curl -T file.txt -u "domain\username:passwd" smb://server.example.com/share/

HTTP

Upload all data on stdin to a specified HTTP site:

curl -T - http://www.upload.com/myfile

Note that the HTTP server must have been configured to accept PUT before this can be done successfully.

For other ways to do HTTP data upload, see the POST section below.

Resume Broken Download

curl -C - url
## OR
curl -L -O -C - url
## OR
curl -L -o 'filename-here' -C - url

Request Header

To apply multiple headers, send them as shown below.

$ curl -H "Content-Type: application/json" -H "Accept: application/json" http://127.0.0.1/

Health check

curl -sSf http://example.org > /dev/null

Flag information:

  -s/--silent
     Silent or quiet mode. Do not show progress meter or error messages.  
     Makes Curl mute.

  -S/--show-error
     When used with -s it makes curl show an error message if it fails.

  -f/--fail
     (HTTP)  Fail silently (no output at all) on server errors. This is mostly
     done to better enable scripts etc to better deal with failed attempts. In
     normal  cases  when a HTTP server fails to deliver a document, it returns
     an HTML document stating so (which often also describes  why  and  more).
     This flag will prevent curl from outputting that and return error 22.

     This method is not fail-safe and there are occasions where non-successful
     response codes will  slip  through,  especially  when  authentication  is
     involved (response codes 401 and 407).

Used POST method

With POST, the message must be sent in the body. You can send data with the -d or --data option. You can also send the contents of a file as POST data. Syntax:

$ curl -X POST -d '내용' 타겟URL
$ curl -X POST -d @파일명  타겟URL

Example:

$ curl -d "name=yundream&age=19&address=Seoul ... ..." http://www.joinc.co.kr/curltest.php
$ curl -X POST http://192.168.57.3:5984/mywiki/ -d @curl.json -H "Content-Type: application/json"

File Data

To add binary data from a file to the Content, use --data-binary. Note that the file name must be prefixed with @.

$ curl -u user:pass -X POST -H "Content-Type: multipart/form-data" --data-binary "@package.deb" http://nexus_url:8081/repository/repo_name/

SFTP

SFTP file upload:

$ curl -T file.txt sftp://user:[email protected]:36651/~/
$ curl -T file.txt scp://user:[email protected]:36651/home/chanik/

Protocol debugging

You can get detailed information about the HTTP data exchange with --verbose, --trace, and --trace-ascii. If --verbose is not enough, use --trace-ascii.

$ curl --trace-ascii debug.dump http://www.test.co.kr/testpage

Cookie information can be sent along as shown below.

curl -b "ASP.NET_SessionId=d8fcb899-c453-4688-8bb1-f1d4993bda5b"  http://mobile.zekitalk.com/typhoon/Typhoon_ListEvent.aspx

Recursive download?

Is there a feature to download web pages recursively? The conclusion is that you should use wget. The relevant passage is below.

Single shot.
curl is basically made to do single-shot transfers of data. It transfers just the URLs that the user specifies, and does not contain any recursive downloading logic nor any sort of HTML parser.

Proxy server

To use a Proxy server, use the -x option. If cURL traffic is not captured in Fiddler, you can solve it as shown below.

curl -x 192.168.0.50:8888 http://mobile.zekitalk.com/typhoon/Typhoon_ListEvent.aspx

Make directory using curl on FTP

curl -p - --insecure  "ftp://82.45.34.23:21/CurlPutTest/test" --user "testuser:testpassword" -Q "-MKD /CurlPutTest/test"  --ftp-create-dirs

Make directory using curl on SFTP

curl  -k "sftp://83.46.38.23:22/CurlPutTest/test " --user "testuser:testpassword" -Q "–MKDIR /CurlPutTest/Test" --ftp-create-dirs

HTTP examples

Print the output including headers.
curl –i [URL]
Print headers only.
curl –I [URL]
Write headers to a file.
curl –D [FILE] [URL]
Save debugging information to a file.
curl --trace-ascii DebugDump.txthttp://www.test.co.kr/testpage
GET data request.

:

# curl http://www.joinc.co.kr
# curl http://www.joinc.co.kr?action=del&id=12345
POST data request.
You can send data with the -d or --data option. Prefixing the file name with @ sends the contents of the file as POST data.

:

# curl -d "name=yundream&age=19&address=Seoul ... ..." http://www.joinc.co.kr/curltest.php
# curl -X POST http://192.168.57.3:5984/mywiki/ -d @curl.json -H "Content-Type: application/json"
Verbose output.
curl -vhttp://www.joinc.co.kr

Extracting the title of a web page

curl -s "https://news.hada.io/topic?id=10569" | grep -oP '(?<=<title>)(.*)(?=</title>)'

FTP examples

List the Child Nodes of FTP.
The important thing is that the path must end with a slash (/).
curlftp://10.10.1.55/dstdir/
To download a file via FTP, use the following.
curl -Oftp://10.10.1.55/dstdir/tmp.txt
To upload a file via FTP, you can simply use the following.
The important thing is that the path must end with a slash (/).
curl -T ~/tmp.txtftp://10.10.1.55/dstdir/
File upload using globbing
curl -T 'image[1-99].jpg'ftp://ftp.example.com/upload/
Uploading multiple files
curl -T '{file1,file2}'ftp://ftp.example.com/upload/
curl -T '{Huey,Dewey,Louie}.jpg'ftp://ftp.example.com/nephews/
When invoking cURL from a script or program and handling only the error messages, you can use the following options.
curlftp://10.10.1.55/dstdir/-s -S
To set a timeout on the operation time, you can use the following option.
curlftp://10.10.1.55/dstdir/-m 5
To set a timeout on the connection time, you can use the following option.
curlftp://10.10.1.55/dstdir/--connect-timeout 3
To automatically create directories on the Remote server, you can pass the following option.
curlftp://10.10.1.55/dstdir/--ftp-create-dirs
To give an input range, specify it with square brackets ([]) as shown below.
curl -Oftp://ftp.numericals.com/file[1-100].txt
To adjust the step when using a range, use a colon (:) as shown below.
curl -Oftp://ftp.numericals.com/file[1-100:10].txt
If the numeric range includes 0, use it as shown below.
curl -Oftp://ftp.numericals.com/file[001-100].txt
Letters are also allowed in ranges.
curl -Oftp://ftp.letters.com/file[a-z].txt

See also

Favorite site

author Zer0 sitemap