How to Use Wget for HTTP PUT and DELETE Requests
While wget is primarily celebrated for downloading files
and mirroring websites, it is fully capable of executing advanced HTTP
methods like PUT and DELETE. This
article provides a quick, practical overview of how to override
wget’s default GET behavior using specific command-line
flags. You will learn the exact syntax required to send custom HTTP
methods, pass data payloads, and interact with RESTful APIs directly
from your terminal.
Using Wget for HTTP PUT Requests
The HTTP PUT method is typically used to upload a file or update an
existing resource on a server. Since wget does not have a
dedicated --method=PUT flag in older versions, the most
universally compatible way to achieve this is by using the
--post-file or --post-data flags, combined
with the --custom-request option to change the method
name.
To send a file using a PUT request, use the following syntax:
wget --custom-request=PUT --post-file="/path/to/local/file.json" --header="Content-Type: application/json" http://example.com/api/resource/1If you prefer to send a raw string of data directly from the command
line instead of a file, swap --post-file for
--post-data:
wget --custom-request=PUT --post-data='{"status":"active"}' --header="Content-Type: application/json" http://example.com/api/resource/1--custom-request=PUT: Forceswgetto change the HTTP request header line from POST to PUT.- **
--post-file/--post-data**: Triggerswgetto support a payload delivery mechanism and correctly calculate content length. --header: Ensures the receiving server understands how to parse the incoming data.
Using Wget for HTTP DELETE Requests
The HTTP DELETE method requests that the origin server delete the resource identified by the Request-URI. This is much simpler to execute than a PUT request because it rarely requires a data payload.
To issue a quick DELETE request to an API endpoint, use the
--custom-request flag:
wget --custom-request=DELETE http://example.com/api/resource/1By default, wget will save the server’s response (even
if it is just a “200 OK” or “204 No Content” confirmation) into a file
in your current directory. If you want to suppress this behavior and
keep your workspace clean, you can redirect the output to the system
trash using the -O flag:
wget --custom-request=DELETE -O /dev/null http://example.com/api/resource/1Important Considerations and Troubleshooting
While wget can get the job done in a pinch, it behaves
strictly as a file-downloading utility at its core. Keep these behaviors
in mind when using it for API testing:
- Handling Redirects: If a PUT or DELETE request
triggers a 301 or 302 redirect,
wgetmay automatically change the subsequent request back to a standard GET method when following the new URL. - Output Files: Every time you run a custom
wgetrequest, it creates a file likeindex.htmlorresource.1locally. Always use-O-to view the response in the terminal, or-O /dev/nullto discard it. - The cURL Alternative: For complex API interactions
involving PUT and DELETE, the
curltool is generally preferred by developers (e.g.,curl -X DELETE http://example.com). However, knowing thewget --custom-requestsyntax ensures you can interact with REST APIs on any minimal Linux environment wherecurlmight not be pre-installed.