cURL stands for client URL. This command is very popular in linux to transfer data to and from a server. It is a command line tool that can be used in Linux, Windows, and Mac O/S.
Using curl you can make rest api calls like:
- GET
- POST
- PUT
- DELETE
- PATCH
Syntax:
The syntax for the curl
command is as follows:
curl [options...] <url>
How do I send a GET request using Curl?
To send get request using curl you can use following syntax:
# basic get request curl https://jsonplaceholder.typicode.com/posts # get request with headers curl https://jsonplaceholder.typicode.com/posts -H "Content-Type: application/json" # fetch only headers using get request curl -I https://jsonplaceholder.typicode.com/posts # curl does not follow redirects however you can tell # curl to follow the redirects using -L option curl -L https://jsonplaceholder.typicode.com/posts # you can send cookies to the server using the -b command-line curl https://jsonplaceholder.typicode.com/posts -b "session=GHIJKLMNOPQRSTUV"
How do I send a POST request using Curl?
To send post request using curl you can use following code:
# send a post request with body curl -X POST https://jsonplaceholder.typicode.com/posts -H 'Content-Type: application/json; charset=UTF-8' -d '{title: 'foo', body: 'bar', userId: 1}' # if you want to send json file as data in your curl you can use curl -X POST https://jsonplaceholder.typicode.com/posts -H 'Content-Type: application/json; charset=UTF-8' -d @post.json # curl post with file curl -d @path/to/data.json https://jsonplaceholder.typicode.com/posts
How to allow insecure HTTPS connections using Curl?
When sending https request curl tries to verify the certificate of the target url against the local CA certificate store. To bypass ssl check use -k option as shown below:
# disable ssl certificate checks curl -k https://jsonplaceholder.typicode.com/posts curl --insecure https://jsonplaceholder.typicode.com/posts # send client certificate to verify ssl curl -E cerfile.crt https://jsonplaceholder.typicode.com/posts curl --cert cerfile.crt https://jsonplaceholder.typicode.com/posts # send ca certificate curl --cacert ca_cert.cert https://jsonplaceholder.typicode.com/posts
How do I download a file using Curl?
To download a file with Curl, use the --output or -o command-line option.
curl -o filename.txt https://reqbin.com/echo
How do I send Basic Auth Credentials with Curl?
Sometime urls are protected with authentication to send basic auth credentials along with your curl request you can use following -u flag:
# send basic auth curl https://some-auth-protected-site.com -u "username:password" # alternatively you can base64 your credentials and use like this curl https://some-auth-protected-site.com -H "Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ="
How do I use Curl with a proxy?
Sometime you may want to use proxy to access remote url. To use a proxy with Curl, you must pass the required proxy address using the -x (or --proxy) command-line option.
# syntax curl -x "[protocol://][host][:port]" [URL] [options] # example curl ttps://some-auth-protected-site.com -x myproxy.com:8080 -U login:password
How to use bearer token in curl?
Sometimes your server allows you to use bearer token for authentication purpose to use bearer token in curl:
# send bearer token as authorization header curl https://jsonplaceholder.typicode.com/posts -H "Accept: application/json" -H "Authorization: Bearer {token}" # send a post request with body curl -X POST https://jsonplaceholder.typicode.com/posts -H 'Content-Type: application/json; charset=UTF-8' -H "Authorization: Bearer {token}" -d '{title: 'foo', body: 'bar', userId: 1}'
How do I send a HEAD request using Curl?
Curl -I option tells curl to only send and HTTP HEAD request to receive only HTTP headers. HEAD request is very similar to GET request however it only returns headers without response body.
# example - 1 curl -I https://reqbin.com/echo # example - 2 curl --head https://reqbin.com/echo # example -3 curl -X HEAD https://reqbin.com/echo
How do I send a DELETE request using Curl?
To make a DELETE request using Curl, you need to use the -X DELETE command-line option:
# syntax curl -X DELETE [URL] [options] # send delete request using curl curl -X DELETE https://jsonplaceholder.typicode.com/posts/1 -H "Accept: application/json"
Curl command options table
Flag | Description | Usage |
---|---|---|
-O | Download the file and save as original name | curl -O [URL] |
-o | Download file and save as diff name | curl -o [file_name] [URL] |
-X | Specify http method like i.e. GET,POST,PUT,DELETE,OPTIONS | curl -X [method] [URL] |
-I | Return only headers | curl -I [URL] |
-k | Ignore SSL verification | curl -k [URL] |
-u | send auth request | curl -u [user:password] [URL] |
-H | send http headers | curl -H "X-Header: value" [URL] |
-v | make verbose | curl -v [URL] |
-L | follow redirects | curl -L [URL] |
-x | send proxy request | curl -x "[protocol://][host][:port]" [URL] [options] |
--cookie | send cookie along with request | curl --cookie "Name=Value" [URL] |
Credits: https://reqbin.com/req/c-dwjszac0/curl-post-json-example