Setting httpConfig.Body at NewRequest to get Content-Length set impliā€¦ (#265)
authorEric K Lee <git@erix.ninja>
Tue, 21 Nov 2017 19:00:01 +0000 (11:00 -0800)
committerBrian Brazil <brian.brazil@robustperception.io>
Tue, 21 Nov 2017 19:00:01 +0000 (19:00 +0000)
prober/http.go

index 88bbb1afcefc777b0311c9aedc397aee33313c10..0d938f8f530658b0997bf64288ef67cc576ec78b 100644 (file)
@@ -255,7 +255,15 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr
        } else {
                targetURL.Host = net.JoinHostPort(ip.String(), targetPort)
        }
-       request, err := http.NewRequest(httpConfig.Method, targetURL.String(), nil)
+
+       var body io.Reader
+
+       // If a body is configured, add it to the request.
+       if httpConfig.Body != "" {
+               body = strings.NewReader(httpConfig.Body)
+       }
+
+       request, err := http.NewRequest(httpConfig.Method, targetURL.String(), body)
        request.Host = origHost
        request = request.WithContext(ctx)
        if err != nil {
@@ -271,10 +279,6 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr
                request.Header.Set(key, value)
        }
 
-       // If a body is configured, add it to the request.
-       if httpConfig.Body != "" {
-               request.Body = ioutil.NopCloser(strings.NewReader(httpConfig.Body))
-       }
        level.Info(logger).Log("msg", "Making HTTP request", "url", request.URL.String(), "host", request.Host)
 
        trace := &httptrace.ClientTrace{