fix: set content-type header before status

This commit is contained in:
Trong Huu Nguyen
2025-02-03 11:38:35 +01:00
parent 055c62db97
commit 2e8c7075ff
2 changed files with 3 additions and 2 deletions

View File

@@ -19,8 +19,8 @@ func DisallowNonNavigationalRequests(next http.Handler) http.Handler {
span := trace.SpanFromContext(r.Context())
span.SetAttributes(attribute.Bool("request.disallowed", true))
w.WriteHeader(http.StatusUnauthorized)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(`{"error": "unauthenticated", "error_description": "this is an interactive endpoint; user-agents must be navigated to this endpoint", "error_path": "` + r.URL.Path + `"}`))
return
}

View File

@@ -203,12 +203,13 @@ func handleAutologin(src ReverseProxySource, w http.ResponseWriter, r *http.Requ
location := loginURL(target, "non-navigation request detected; responding with 401 and Location header")
w.Header().Set("Location", location)
w.WriteHeader(http.StatusUnauthorized)
if httpinternal.Accepts(r, "*/*", "application/json") {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(`{"error": "unauthenticated, please log in"}`))
} else {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("unauthenticated, please log in"))
}
}