fix transport http headers

This commit is contained in:
2026-07-08 10:19:48 +02:00
parent 82c6d2e7bb
commit 5e569b91fc

View File

@@ -53,10 +53,7 @@ func (t *DebugTransport) RoundTrip(req *http.Request) (*http.Response, error) {
contentline = buf.String() contentline = buf.String()
} }
if req.Header.Get("Accept") != "" { req = fixRequestHeaders(req)
req.Header.Del("Accept")
req.Header.Add("Accept", "application/json")
}
slog.Info("req", "host", req.URL.Host, "uri", req.URL.Path, slog.Info("req", "host", req.URL.Host, "uri", req.URL.Path,
"body", content, "bodyline", contentline, "body", content, "bodyline", contentline,
@@ -66,20 +63,31 @@ func (t *DebugTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return t.Transport.RoundTrip(req) return t.Transport.RoundTrip(req)
} }
// Fixes https://codeberg.org/scip/esctl/issues/79:
// the API client has this Accept header hardcoded everywhere:
// req.Header.Set("Accept", "application/vnd.elasticsearch+json;compatible-with=9")
// While this works pretty well with ES9, it doesn't with ES8. So, we replace
// this header with a new one without the compatibility part.
type CompatibilityTransport struct { type CompatibilityTransport struct {
Transport http.RoundTripper Transport http.RoundTripper
} }
func (t *CompatibilityTransport) RoundTrip(req *http.Request) (*http.Response, error) { func (t *CompatibilityTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req = fixRequestHeaders(req)
return t.Transport.RoundTrip(req)
}
// Fixes https://codeberg.org/scip/esctl/issues/79:
// the API client has this Accept header hardcoded everywhere:
// req.Header.Set("Accept", "application/vnd.elasticsearch+json;compatible-with=9")
// While this works pretty well with ES9, it doesn't with ES8. So, we replace
// this header with a new one without the compatibility part.
func fixRequestHeaders(req *http.Request) *http.Request {
if req.Header.Get("Accept") != "" { if req.Header.Get("Accept") != "" {
req.Header.Del("Accept") req.Header.Del("Accept")
req.Header.Add("Accept", "application/json") req.Header.Add("Accept", "application/json")
} }
return t.Transport.RoundTrip(req) if req.Header.Get("Content-Type") != "" {
req.Header.Del("Content-Type")
req.Header.Add("Content-Type", "application/json")
}
return req
} }