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.
Curl in a corporate environment
How to use sdkman in wsl Ubuntu? The curl command is not installing it.
If the curl command is failing to install SDKMAN in your WSL Ubuntu environment, the issue might not be with SDKMAN itself, but rather with the curl or zip utilities or network connectivity within your WSL instance.
Troubleshooting SDKMAN installation in WSL Ubuntu:
Ensure curl and zip are installed. SDKMAN relies on curl for downloading and zip/unzip for extracting. Verify their presence and install them if missing:
sudo apt update
sudo apt install curl zip unzip
Verify Network Connectivity
Confirm your WSL Ubuntu instance can access the internet, as SDKMAN needs to download files from its servers. You can test this with a simple curl command:
curl -I https://get.sdkman.io
(-i, --include) Include protocol response headers in the output
If this fails, investigate your WSL network configuration or any proxy settings that might be interfering. Install SDKMAN. Once curl and zip are confirmed and network connectivity is established, you can proceed with the SDKMAN installation: curl -s “https://get.sdkman.io” | bash
If you are behind a corporate proxy
You might need to configure curl to use your proxy. This can often be done by setting environment variables or modifying the curl command directly. For example:
export http_proxy="http://your_proxy_address:port"
export https_proxy="http://your_proxy_address:port"
curl -s "https://get.sdkman.io" | bash
Remember to replace your_proxy_address:port with your actual proxy details. You might also need to add proxy settings within the SDKMAN configuration files after installation, as mentioned in some older discussions.