Standard library / net/http

http.ErrUseLastResponse

Special redirect-policy sentinel: return the last response with its body still open and no redirect error. Expected stdout: '302\n'.

Formal Syntax & Structure

go syntaxgo
var ErrUseLastResponse error

Working Example

Example Snippetgo
package main
import (  "fmt"  "net/http"  "net/http/httptest")
func main() {  s := fixture()  defer s.Close()  c := &http.Client{CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}  r := must(c.Get(s.URL + "/redirect"))  defer r.Body.Close()  fmt.Println(r.StatusCode)}func fixture() *httptest.Server {  return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {    switch r.URL.Path {    case "/redirect":      http.Redirect(w, r, "/done", http.StatusFound)    case "/cookie":      http.SetCookie(w, &http.Cookie{Name: "demo", Value: "yes", Path: "/", HttpOnly: true})    case "/echo":      c, e := r.Cookie("demo")      if e == nil {        fmt.Fprint(w, c.Value)      }    default:      fmt.Fprint(w, "hello")    }  }))}
func must[T any](v T, e error) T {  if e != nil {    panic(e)  }  return v}func check(e error) {  if e != nil {    panic(e)  }}
Go Reference