CURL:Examples
lastmod 2024-07-23created 2020-07-0911 edits
Examples using cURL.
Simple Usage
Get the main page from a web-server:
Get a README file from an FTP server:
Get a web page from a server using port 8000:
Get a directory listing of an FTP site:
Get the all terms matching curl from a dictionary:
Get the definition of curl from a dictionary:
Fetch two documents at once:
Get a file off an FTPS server:
or use the more appropriate FTPS way to get the same file:
Get a file from an SSH server using SFTP:
Get a file from an SSH server using SCP using a private key (not password-protected) to authenticate:
Get a file from an SSH server using SCP using a private key (password-protected) to authenticate:
Get the main page from an IPv6 web server:
Get a file from an SMB server:
Uploading
FTP / FTPS / SFTP / SCP
Upload all data on stdin to a specified server:
Upload data from a specified file, login with user and password:
Upload a local file to the remote site, and use the local file name at the remote site too:
Upload a local file to get appended to the remote file:
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:
SMB / SMBS
HTTP
Upload all data on stdin to a specified HTTP site:
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
Request Header
To apply multiple headers, send them as shown below.
Health check
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:
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.
Sending cookie information
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.
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.
:
- POST data request.
- You can send data with the
-dor--dataoption. 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
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