Why does WP HTTP API switch the method (POST/PURGE) to GET when redirecting (302)?
I discovered after much painful debugging that requests to the the WP HTTP API (in this case, though wp_remote_request()
) always ended up as GET
method after being redirected, even if the method was something else in the initial request (in my case PURGE
as used by the Varnish HTTP Purge plugin)
Normally this would apply to POST
requests, where redirecting to a GET
request of the same URL means completely obliterating the data being sent with the POST
. In my case, using PURGE
the outcome was that Apache was loading the actual URLs I was trying to purge, which wasn't what I wanted.
The point being: Pretty much never will you want this to happen. Whatever method you use to send a request, you surely want that method to be used in the end. This behavior is confusing and annoying and will probably be experienced as inexplicable bugginess for most users (in my case, the bug took me years to track down, and had been slowing down my local dev site where I don't have Varnish installed).
I'm posting this question so I can answer it myself in hopes people find it using Google in the future. The point is to be aware of this behavior so that if it is happening to you you can just find a way around it.