Following redirects with curl

I was testing how redirect-after-POST works with Google App Engine’s urlfetch.fetch function and got the surprising result that it follows a redirect from a POST request with…another POST request. This is not what browsers do so I tried it with curl.

curl -L -XPOST http://example.com/some-form-handler

I was confused to see that curl also had this behavior, since the man page says:

When curl follows a redirect and the request is not a plain GET (for example POST or PUT), it will do the following request with a GET if the HTTP response was 301, 302, or 303. If the response code was any other 3xx code, curl will re-send the following request using the same unmodified method.

Brian Gilstrap also ran into this and got an answer from a commenter, which is documented in this bug.

The trick is that the -X argument tells curl to always use the specified HTTP method. To get curl to switch from POST to GET, you need to make curl implicitly do a POST, for example with the -d flag.

I find this counter-intuitive, but at least there is a work-around!