Send GET Request using wget

Send GET Request using wget

Sending HTTP requests is essential in web development and API interaction. A widely used command line tool for making these requests is wget. HTTP GET requests specifically retrieve data from a specified resource, such as a webpage or an API endpoint. This tutorial explains how to send a GET request using wget.

Simple GET request

To perform an HTTP GET request to the specified URL, use the following command:

wget -qO- https://httpbin.org/get

The -q option stands for "quiet", making the command to run silently without displaying download progress or error messages. The -O- option tells wget to output the response directly to the standard output (stdout), which is typically the terminal, rather than saving it to a file.

In this scenario, the output you receive will be in JSON format and will look similar to the following:

{
  "args": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "identity", 
    "Host": "httpbin.org", 
    "User-Agent": "Wget/1.21.2", 
    "X-Amzn-Trace-Id": "Root=1-6416c45e-47a4828a557d5fca764271d6"
  }, 
  "origin": "XXX.XXX.XXX.XXX", 
  "url": "https://httpbin.org/get"
}

Add query parameters

If the URL requires query parameters, you can add them directly to the URL like this:

wget -qO- "https://httpbin.org/get?param1=value1&param2=value2"

Be sure to enclose the URL in double quotes to avoid issues with special characters.

View response headers

To view only the response headers, use the following command:

wget -S -qO /dev/null https://httpbin.org/get

The -S option instructs wget to print the HTTP response headers to the standard error output (stderr). The -O option directs wget to discard the actual response body by sending it to /dev/null, effectively making sure that only the headers are displayed.

The output could look something like this:

HTTP/1.1 200 OK
Date: Mon, 27 May 2024 16:09:20 GMT
Content-Type: application/json
Content-Length: 292
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

This is useful for checking headers such as content type and server information.

Save output

To save the output of the GET request to a file, use the -O option:

wget -qO response.json https://httpbin.org/get

This command will store the retrieved content into a file named response.json.

Leave a Comment

Cancel reply

Your email address will not be published.