Skip to content

Commit

Permalink
Add /headers endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
federicotdn committed Jan 20, 2025
1 parent fb2a361 commit d1f129c
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pkg/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,27 @@ func (s *Server) AddHTTPTesting() {
w.WriteHeader(http.StatusOK)
})

r.Get("/api/headers", func(w http.ResponseWriter, r *http.Request) {
headers := map[string]string{}

for key, values := range r.Header {
headers[key] = strings.Join(values, ",")
}
headers["Host"] = r.Host

buf := bytes.Buffer{}
err := json.NewEncoder(&buf).Encode(map[string]any{"headers": headers})
if err != nil {
s.log.ErrorContext(r.Context(), "Failed to encode response", "err", err)
w.WriteHeader(http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(buf.Bytes())
})

r.Get("/api/basic-auth/{username}/{password}", func(w http.ResponseWriter, r *http.Request) {
user, pass, _ := r.BasicAuth()
username := chi.URLParam(r, "username")
Expand Down

0 comments on commit d1f129c

Please sign in to comment.