How to Keep Same HTTP Method After Curl Redirect
When using cURL to follow HTTP redirects, the tool’s default behavior
often converts POST requests into GET requests. This article explains
how to use the specific cURL options --post301,
--post302, and --post303 to maintain the
original HTTP POST method across redirects.
By default, when you use the -L or
--location option to follow redirects, cURL adheres to
historical browser behavior. If a server responds with a 301, 302, or
303 redirect status code in response to a POST request, cURL will
automatically convert the subsequent request to a GET request.
To force cURL to maintain the POST method after a redirect, you must use specific command-line options depending on the HTTP status code returned by the server:
--post301: Tells cURL to respect RFC 7231 and not convert POST requests into GET requests when following a 301 (Moved Permanently) redirection.--post302: Tells cURL to respect RFC 7231 and not convert POST requests into GET requests when following a 302 (Found) redirection.--post303: Tells cURL to respect RFC 7231 and not convert POST requests into GET requests when following a 303 (See Other) redirection.
Example Usage
To send a POST request and ensure the POST method is maintained if the server redirects with either a 301 or 302 status code, you can combine the flags with the location option:
curl -L --post301 --post302 -d "param1=value1" https://example.com/apiFor non-POST methods (such as PUT or DELETE), cURL preserves the HTTP method during redirects automatically. These specific options are only required to prevent POST from being converted to GET.