How to Send Custom HTTP Methods with cURL

Yes, cURL can be used to send custom HTTP methods. While cURL defaults to standard methods like GET, POST, and PUT, you can easily initiate requests using any custom or WebDAV HTTP verb—such as PURGE, MERGE, or COPY—by utilizing the -X or --request command-line flag. This article provides a straightforward guide on how to use this flag, provides practical examples, and explains key behaviors to keep in mind when overriding default request methods.

The -X and --request Option

To send a custom HTTP method, append the -X flag (or the equivalent long-form option --request) followed by your desired custom verb, and then specify the target URL.

The basic syntax is as follows:

curl -X METHOD URL

Example: Sending a PURGE Request

Many caching servers, such as Varnish, support a custom PURGE method to clear cached content. To send a PURGE request to a specific URL, use:

curl -X PURGE https://example.com/cached-page

Example: Sending Data with a Custom Method

If your custom method requires a payload (such as a JSON body), you can combine the -X option with the -d (data) and -H (header) options:

curl -X MERGE -H "Content-Type: application/json" -d '{"status": "active"}' https://api.example.com/resource

Important Considerations

When using custom HTTP methods with cURL, keep the following rules in mind: