From: Eric K Lee Date: Tue, 21 Nov 2017 19:00:01 +0000 (-0800) Subject: Setting httpConfig.Body at NewRequest to get Content-Length set impli… (#265) X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=f0be77c4056ed29c7170d8f85be95d30bd88b9bd;p=blackbox_exporter.git Setting httpConfig.Body at NewRequest to get Content-Length set impli… (#265) --- diff --git a/prober/http.go b/prober/http.go index 88bbb1a..0d938f8 100644 --- a/prober/http.go +++ b/prober/http.go @@ -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{