curl

From RaySoft

curl is a command line tool for transferring files with URL syntax, supporting FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, LDAP and LDAPS. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, user and password authentication (Basic, Digest, NTLM, Negotiate, Kerberos...), file transfer resume, proxy tunneling and a busload of other useful tricks.[1]

Documentation

Syntax

curl [PARAMETER ...] [URL ...]

Parameters

-d DATA, --data DATA
(HTTP) Sends the specified DATA in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded.
If you start the data with the letter @, the rest should be a file name to read the data from, or - if you want curl to read the data from stdin. The contents of the file must already be curl URL-encoded.
--data-urlencode DATA
(HTTP) This posts DATA, similar to the other --data options with the exception that this performs URL-encoding.
To be CGI-compliant, the DATA part should begin with a name followed by a separator and a content specification. The DATA part can be passed to curl using one of the following syntaxes:
content
This will make curl URL-encode the content and pass that on. Just be careful so that the content doesn't contain any = or @ symbols, as that will then make the syntax match one of the other cases below!
=content
This will make curl URL-encode the content and pass that on. The preceding = symbol is not included in the data.
name=content
This will make curl URL-encode the content part and pass that on.
NOTE:
The name part is expected to be URL-encoded already.
@filename
This will make curl load data from the given file (including any newlines), URL-encode that data and pass it on in the POST.
name@filename
This will make curl load data from the given file (including any newlines), URL-encode that data and pass it on in the POST. The name part gets an equal sign appended, resulting in name=urlencoded-file-content.
NOTE:
The name is expected to be URL-encoded already.
-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 an 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.
-F name=content[,...], --form name=content[,...]
(HTTP) This lets curl emulate a filled in form in which a user has pressed the submit button. This causes curl to POST data using the content-type multipart/form-data according to RFC1867.
--ftp-create-dirs
(FTP & SFTP) When an FTP operation uses a path that doesn't currently exist on the server, the standard behavior of curl is to fail. Using this option, curl will instead attempt to create missing directories.
-H HEADER, --header HEADER
(HTTP) Extra HEADER to use when getting a web page. You may specify any number of extra headers.
-i, --include
(HTTP) Include the HTTP-header in the output. The HTTP-header includes things like server-name, date of the document, HTTP-version and more...
-k, --insecure
(TLS) By default, every SSL connection curl makes is verified to be secure. This option allows curl to proceed and operate even for server connections otherwise considered insecure.
-K FILE|-, --config FILE|-
Specify which config FILE to read curl arguments from. The config file is a text file in which command line arguments can be written which then will be used as if they were written on the actual command line.
Specify the filename to -K, --config as - to make curl read the file from stdin.
-L, --location
(HTTP) If the server reports that the requested page has moved to a different location (indicated with a Location: header and a 3XX response code), this option will make curl redo the request on the new place.
--mail-from ADDRESS
(SMTP) Specify a single ADDRESS that the given mail should get sent from.
--mail-rcpt ADDRESS
(SMTP) Specify a single ADDRESS that the given mail should get sent to. This option can be used multiple times to specify many recipients.
-o FILE, --output FILE
Write output to FILE instead of stdout. If you are using {} or [] to fetch multiple documents, you can use # followed by a number in the FILE specifier. That variable will be replaced with the current string for the URL being fetched.
-s, --silent
Silent or quiet mode. Don't show progress meter or error messages. Makes curl mute.
--ssl-reqd
(FTP, POP3, IMAP & SMTP) Require SSL/TLS for the connection. Terminates the connection if the server doesn't support SSL/TLS.
-T FILE, --upload-file FILE
This transfers the specified local FILE to the remote URL. If there is no file part in the specified URL, curl will append the local FILE name.
-u USER[:PASSWORD], --user USER[:PASSWORD]
Specify the USER name and PASSWORD to use for server authentication.
-X COMMAND, --request COMMAND
(HTTP) Specifies a custom request method to use when communicating with the HTTP server. The specified request will be used instead of the method otherwise used (which defaults to GET). Read the HTTP 1.1 specification for details and explanations. Common additional HTTP requests include PUT and DELETE, but related technologies like WebDAV offers PROPFIND, COPY, MOVE and more.
Specifies a custom FTP command to use instead of LIST when doing file lists with FTP.

Examples

Hide username and password from being visible in process tables
echo 'user = alex:********' | curl \
  --silent \
  --config - \
  'https://www.raysoft.ch/'
Delete a file from a FTP server
echo 'user = alex:********' | curl \
  --silent \
  --config - \
  --request 'DELE test.tar.gz' \
  'ftp.raysoft.loc/myfiles'
Upload a file on a FTP server
echo 'user = alex:********' | curl \
  --silent \
  --config - \
  --ftp-create-dirs \
  --upload-file 'test.tar.gz' \
  'ftp.raysoft.loc/myfiles'
Pipe command line output directly to dpaste
curl \
  --include \
  --silent \
  --form 'title=Test dpaste' \
  --form 'poster=Alex' \
  --form 'expiry_days=1' \
  --form 'syntax=python' \
  --form 'content=<-' \
  'https://dpaste.com/api/v2' \
| grep '^Location:'

Output:

Location: http://dpaste.com/381A1H3
Send an email
curl 'smtps://smtp.gmail.com:587' \
  --silent \
  --ssl-reqd \
  --user 'alex.the.lion@gmail.com:**********' \
  --mail-from 'alex.the.lion@gmail.com' \
  --mail-rcpt 'marty.the.zebra@gmail.com' \
  --upload-file - <<-EOM
		From: Alex the Lion <alex.the.lion@gmail.com>
		To: Marty the Zebra <marty.the.zebra@gmail.com>
		Date: $(date +'%-d %b %Y %T %z')
		Subject: Test

		This is a test email.

		C u, Lion
	EOM

if [[ $? -ne 0 ]]; then
  echo "ERROR: Couldn't send email!"
fi
Get my public IP
if type -P 'curl' >'/dev/null' 2>&1; then
  alias my::ip='echo -n "My public IP is " && curl "http://ifconfig.me/"'
fi

References