cURL
cURL is a command line tool for transfering data. cURL supports the common http, https, ftp, ftps, telnet, pop3, ldap, and many other protocol. I use this for troubleshooting or use in a console where UI is not available. cURL supports sending in any http method (i.e. GET, POST, PUT, DELETE) and also supports sending in the post data. cURL allows setting http request headers.
Getting started
-
Check if you already have cURL by running this command in the regular OS console.
curl --version
-
If you do not have cURL installed, go to https://curl.se/download.html and download it for your OS. They cover the Windows, MacOS, Linux, and other OSes.
Basics
-
A basic usage of the curl is a simple GET request.
curl https://localhost:44304/api/example
-
A post http method operation can be performed. Use the option -X and then add in the http method, in this case POST. We can add in the post body by using the option -d. On Windows, when sending in post body data, it is necessary to put escape character before the quotation mark like this \".
The content type also needs to be set for the post body data. For json payload, we use Content-Type: application/json and this needs to be added as a header with the option -H.
The following example is sending in a json payload in the body.
curl -X POST https://localhost:44304/api/example -d "{ \"key1\": \"value1\"}" -H "Content-Type: application/json"
-
A PUT http method can be performed by using the option -X PUT. We can pass in the data as we would in a POST http method by using -d option.
curl -X PUT https://localhost:44304/api/example -d "{ \"key1\": \"value1\"}" -H "Content-Type: application/json"
-
A DELETE http method can be performed by using the option -X DELETE.
curl -X DELETE https://localhost:44304/api/example/5
Other options
-
We can use the cURL's verbose option, -v, to get more information on the communication between cURL tool and the server.
curl https://localhost:44304/api/example -v
-
If we need to only look at the response headers instead of the full verbose logging, we can use the -i option.
curl https://localhost:44304/api/example -i
-
If we are connecting to a server that has self signed certificate or untrusted certificate, we would get an error like this:
If we are sure we are connecting to the correct server but want to ignore this SSL certificate warning about the self signed certificate or untrusted certificate, we can use the -k option. For more details, please go to https://curl.se/docs/sslcerts.html
curl https://localhost:44304/api/example -k
Published on Last updated on