Curl
Curl stands for “Client for URLs”
References
https://everything.curl.dev/http
In every HTTP request, there is a method. Sometimes called a verb. The most commonly used ones are GET, POST, HEAD and PUT.
Normally however you do not specify the method in the command line, but instead the exact method used depends on the specific options you use. GET is default, using -d or -F makes it a POST, -I generates a HEAD and -T sends a PUT.
GET
Since GET is the default, we don’t have to specifically mention the HTTP type in a GET request.
curl --verbose http://localhost:8080/list/files
curl --verbose https://api.publicapis.org/entries
POST
curl --request "POST" -H "Content-Type: application/json" -d '{"name": "linuxize", "email": "linuxize@example.com"}' https://endpoint.com
or
curl -X POST -H "Content-Type: application/json" -d '{"name": "linuxize", "email": "linuxize@example.com"}' https://endpoint.com
https://everything.curl.dev/http/post
PUT
https://everything.curl.dev/http/put
DELETE
curl --request "DELETE" http://www.example.com/page -v | json_pp
Display request and response headers on terminal
We can use curl -v
or curl -verbose
to display the request headers and response headers in the cURL command.
In the cURL response
The > lines are request headers. The < lines are response headers.
Example:
[h@h-p50-20eqs27p03 springboot-mongodb-crud-rest-api]$ curl -v http://localhost:8080/users
* Trying 127.0.0.1:8080...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /users HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.85.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200
< Content-Type: application/json
< Transfer-Encoding: chunked
< Date: Wed, 19 Oct 2022 03:25:02 GMT
<
* Connection #0 to host localhost left intact
[{"id":"634f6c60e5d2eb3a186b05d3","username":"myself","correctAnswer":"right","numberOfTries":0,"attemptedWords":null},{"id":"634f6de9e5d2eb3a186b05d4","username":"myself","correctAnswer":null,"numberOfTries":0,"attemptedWords":null}]
How to Pretty Print JSON output in cURL
Prerequisite (make sure this is installed)
https://command-not-found.com/json_pp
In cURL requests, the default JSON output is in compact format.
In cURL, we can use or pipe the json_pp to pretty print the JSON output.
curl https://api.cloudflare.com/client/v4/ -verbose | json_pp
How to measure response times?
In the home directory, set-up curl-format.txt
similar to this one: https://github.com/explorer436/Dotfiles2/blob/main/curl-format.txt
Note that the commands to give the time taken will not give the response body. I haven’t figured out how to make the two of them work together.
The commands to run to get the time statistics are different from the ones used to receive response bodies.
This one worked.
curl -o /dev/null -s -w 'Total: %{time_total}s\n' "https://api.publicapis.org/entries"
Didn’t work.
curl -w ~/.curl-format.txt -o /dev/null -s "https://api.publicapis.org/entries"
Haven’t tried.
curl -X POST -d @file server:port -w %{time_connect}:%{time_starttransfer}:%{time_total}
All of the variables used with -w can be found in man curl.