How do I send a GET request using Curl? [Java Code]
To make a GET request using Curl, run the curl command followed by the target URL. Curl automatically selects the HTTP GET request method unless you use the -X, --request, or -d command-line option. In this Curl GET example, we send Curl requests to the ReqBin echo URL. The target URL is passed as the first command-line option. No additional Curl parameters are passed since in this Curl GET example, and we don't want to send additional HTTP headers to the server. The Java code was automatically generated for the Curl GET Request example.
Curl stands for Client for URLs, and it is a popular command-line tool for Linux, Windows, and macOS for transferring data over the network using HTTP, HTTPS, FTP, and SFTP protocols. You can make GET, POST, and HEAD requests to the server, retrieve HTTP headers, download HTML pages, upload files, submit forms, and more.
Basic Curl GET request example
Curl is effortless to use, and this basic Curl GET example demonstrates how easy it is to make a GET request to the target server using Curl.
Basic Curl GET request example
How to send HTTP headers with a Curl GET request?
To make a GET request with HTTP headers, use the -H command-line option. You can pass as many HTTP headers with your Curl GET request as you like using the -H command line multiple times.
To fetch only HTTP headers, use the -I command-line option. In this case, Curl will use the HTTP HEAD method instead of the GET request method and will not download the body of the HTTP response message.
Curl GET HTTP headers example
curl -I https://reqbin.com/echo
How to check if the target URL supports HTTP/2 using Curl?
By sending a Curl HEAD request along with the --http2 command line parameter, you can check if the target URL supports the HTTP/2 protocol.
Curl HTTP/2 support check
curl -I --http2 https://reqbin.com/echo
In the response, you will see the HTTP/2 200 status line if your server supports the HTTP/2 protocol or HTTP/1.1 200 otherwise.
How to tell Curl to follow redirects?
By default, Curl doesn't follow 300x redirects. You can force Curl to follow the redirects given in the Location header using the -L command-line option.
Curl GET Request Example with Follow Redirects option
curl -L https://reqbin.com/echo
How to get a specific range of bytes from a resource using Curl?
You can use the -r command-line option to get a specific range of resource bytes from the target URL.
Curl example to get a specific range of bytes
How to send cookies along with a GET request using Curl?
You can send cookies to the server using the -b command-line option followed by a string with the cookie or the name of the file containing the cookies.
In this Curl get JSON example, the Accept: application/json header is essential because, without this header, the server might return data in a different format.
An example of downloading an HTML page from the ReqBin echo URL.
Curl GET HTML page example
In this Curl GET HTML page example, we did not pass any additional headers. Curl automatically adds an Accept: */* request header that tells the server that the Curl client can accept data in any format.
Server response to our Curl get HTML page request.
Server response to Curl request
HTTP/1.1 200 OK
[html code here]
Limiting the maximum transfer rate for Curl GET requests
With the --limit-rate command-line parameter, you can limit the maximum transfer rate for uploading and downloading files. Speed is measured in bytes per second unless you specify otherwise with the suffix K (kilobytes), M (megabytes), or G (gigabytes).