From 86fd5b4c97faa12f2dd5507c98105a3a50868074 Mon Sep 17 00:00:00 2001 From: Bilal Amarni Date: Thu, 2 Feb 2017 17:09:47 +0100 Subject: [PATCH] Set a NopCloser request body with retry middleware As the http client always closes the request body, this makes sure the request can be retried if needed. Fixes #1008 --- middlewares/retry.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/middlewares/retry.go b/middlewares/retry.go index e290963b4..21568eb97 100644 --- a/middlewares/retry.go +++ b/middlewares/retry.go @@ -3,6 +3,7 @@ package middlewares import ( "bufio" "bytes" + "io/ioutil" "net" "net/http" @@ -32,6 +33,13 @@ func NewRetry(attempts int, next http.Handler) *Retry { } func (retry *Retry) ServeHTTP(rw http.ResponseWriter, r *http.Request) { + // if we might make multiple attempts, swap the body for an ioutil.NopCloser + // cf https://github.com/containous/traefik/issues/1008 + if retry.attempts > 1 { + body := r.Body + defer body.Close() + r.Body = ioutil.NopCloser(body) + } attempts := 1 for { recorder := NewRecorder()