Send POST Request using wget

Send POST Request using wget

Interacting with web services and APIs often requires sending HTTP requests. Among the various tools available for this purpose, wget stands out as a versatile command line utility. It supports sending different types of HTTP requests, including HTTP POST requests. These POST requests are commonly used to submit data to a specified resource, such as submitting form data to a server or creating a new resource at an API endpoint. This tutorial shows how to send POST request using wget.

Simple POST request

To perform a simple POST request, use the following command:

wget -qO- --post-data "name=John&age=20" https://httpbin.org/post

The -q option suppresses the output of wget, making it run quietly, while -O- directs the response body to the standard output (usually the terminal) instead of saving it to a file. The --post-data option specifies the data to be sent in the request body.

In this example, the response you receive will be formatted in JSON, similar to this:

{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "age": "20", 
    "name": "John"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "identity", 
    "Content-Length": "16", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "Wget/1.21.2", 
    "X-Amzn-Trace-Id": "Root=1-6416c45e-47a4828a557d5fca764271d6"
  }, 
  "json": null, 
  "origin": "XXX.XXX.XXX.XXX", 
  "url": "https://httpbin.org/post"
}

JSON data

To send JSON data in an HTTP POST request, use the --post-data option to specify the data and the --header option to set the appropriate headers.

wget -qO- --header "Content-Type:application/json" --post-data "{\"name\":\"John\"}" https://httpbin.org/post

Save output

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

wget -qO response.json --post-data "name=John&age=20" https://httpbin.org/post

Leave a Comment

Cancel reply

Your email address will not be published.