blackbox_exporter icon indicating copy to clipboard operation
blackbox_exporter copied to clipboard

support Content-Length

Open s5unty opened this issue 9 months ago • 1 comments

Even when a user sets the Content-Length header, the HTTP request still uses the Transfer-Encoding: chunked header and ignores the user-specified Content-Length.

When I used blackbox_exporter to send a POST request to an older lighttpd server, I encountered a 411 error.

The following patch, after simple testing, can resolve this issue and hopefully will be helpful.

 prober/http.go | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git prober/http.go prober/http.go
index 864bf93..fe4c20e 100644
--- prober/http.go
+++ prober/http.go
@@ -438,6 +438,15 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr
                        continue
                }
 
+               if textproto.CanonicalMIMEHeaderKey(key) == "Content-Length" {
+                       length, err := strconv.ParseInt(value, 10, 64)
+                       if err != nil {
+                               logger.Error("Error creating request", "err", err)
+                               return
+                       }
+                       request.ContentLength = length
+               }
+
                request.Header.Set(key, value)
        }
 

s5unty avatar Mar 04 '25 01:03 s5unty

HTTP request still uses the Transfer-Encoding: chunked header and ignores the user-specified Content-Length.

not sure if I understand the issue here, seems like expected behaviour.

I am reading more on it and it says following: source MDN

The Content-Length header must be omitted, and at the beginning of each chunk, a string of hex digits indicate the size of the chunk-data in octets

can you try setting Transfer-Encoding: to something else and it should respect the user-specified Content-Length.

electron0zero avatar Oct 30 '25 19:10 electron0zero