Transfers data from or to a server via curl command and it supports many protocols, including HTTPS, FTP, SMTP, IMAP, POP3 to name a few.
Options
Options are the real power of CURL. Here I’ll cover the most common options.
-
-o / --output <FILE>
Download a file from the given URL to a specific filename. -
-O
Download a file and get the filename indicated by the URL. -
-s / --silent
Run silently or don’t show progress meter while downloading a file. -
-I / --head
Response only headers in the output. -
-i / --include
Include the headers response in the output. -
-D / --dump-header <FILE>
Save the headers response to a separate file. -
-k / --insecure
Skip SSL certificate verification (if your certificate is expired then you may use this option to ignore SSL verification). -
--compressed
Add the HTTP(s) header to request compressed content or data. -
-H / --header <HEADER>
Set a custom HTTP(s) header. Click here to understand better. -
-X / --request <METHOD>
Set custom HTTP(s) method (GET
,PUT/PATCH
,POST
, andDELETE
). Click here to understand better. -
-d / --data <DATA>
Set data to be sent with a POST request. Click here to understand better. -
-A / --user-agent <AGENT>
Set a custom HTTP(s) User-Agent, if you don’t want the default curl option. -
-x / --proxy <HOST:PORT>
Route data through the given proxy (your data will go through proxy). -
--user <USER:PASS>
Pass a username and password for server authentication. -
--cert client.pem --key key.pem --insecure
Pass client certificate and key for a resource, and skip certificate validation.
Get options listed above and put them one by one to the following structure.
1
2
3
4
curl [options] <URL>
# example
curl -o filename.json https://httpbin.org/json
You are allowed to add multiple options as well, for example.
1
curl -I -O https://httpbin.org/json
You can get the full set of options on your system by running the following commands curl --help
or curl --manual
but the most commons are listed above.
Let’s have a look at a bit tricky part of the curl command, which you need to know.
Custom Headers
Click here to find out MIME Types, If you don’t remember.
Custom header to specific endpoint or URL.
1
curl --header 'X-My-Header: 123' https://httpbin.org/json
By setting Content-Type means your header will look at the content type and trying to match with the file’s MIME Type.
1
curl --header 'Content-Type: application/json' https://httpbin.org/json
Sometimes setting authorization for Bearer or Basic becomes a bit tricky, that is why I have created a demo for you to understand the code much cleaner way.
1
2
3
4
5
6
7
8
9
# Bearer Authorization with token
curl --header 'Authorization: Bearer <TOKEN>' https://example.com/login
# Basic Authorization with Username and Password
USERNAME=user
PASSWORD=passwd
CREDENTIAL=$USERNAME:$PASSWORD
BASE64_CODE=$(echo -n $CREDENTIAL | base64) # dXNlcjpwYXNzd2Q=
curl --user $CREDENTIAL -H "Authorization: Basic $BASE64_CODE" https://example.com/login
Request Methods
By Default, the curl command uses the GET
method, but this is how you can set more methods whenever needed.
Note: You are not allowed to set multiple request options (or cannot repeat), rest options can still be repeated.
1
2
# request and response with GET method
curl --request GET https://httpbin.org/uuid
Send data in JSON format, specifying the appropriate content-type header.
1
2
3
4
curl --request POST https://httpbin.org/post --header 'Content-Type: application/json' --data '{"first_name": "Yafiz", "last_name": "Abraham"}'
# without specifying content-type.
curl --request POST https://httpbin.org/post --data 'first_name=Yafiz' --data 'last_name=Abraham'
For more information visit https://curl.se and check HTTP(s) Status Codes.