diff --git a/src/clientlayers/RedirectRequest.jl b/src/clientlayers/RedirectRequest.jl index 33e44234c..60303833c 100644 --- a/src/clientlayers/RedirectRequest.jl +++ b/src/clientlayers/RedirectRequest.jl @@ -101,6 +101,16 @@ function newmethod(request_method, response_status, redirect_method) return request_method elseif redirect_method !== nothing && String(redirect_method) in ("GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS", "PATCH") return redirect_method + elseif request_method == "HEAD" + # Unless otherwise specified (e.g. with `redirect_method`), be conservative and keep the + # same method, see: + # + # * + # * + # + # Turning a HEAD request through a redirect may be undesired: + # . + return request_method end return "GET" end diff --git a/test/client.jl b/test/client.jl index cdf637b6a..023744e26 100644 --- a/test/client.jl +++ b/test/client.jl @@ -208,6 +208,15 @@ end # canonicalizeheaders @test isok(HTTP.get("https://$httpbin/ip"; canonicalizeheaders=false, socket_type_tls=tls)) + + # Ensure HEAD requests stay the same through redirects by default + r = HTTP.head("https://$httpbin/redirect/1") + @test r.request.method == "HEAD" + @test iszero(length(r.body)) + # But if explicitly requested, GET can be used instead + r = HTTP.head("https://$httpbin/redirect/1"; redirect_method="GET") + @test r.request.method == "GET" + @test length(r.body) > 0 end end