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/1

If 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

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/1

By 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/1

Important 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: