From 614b7b4e73582f08c06f66acefe4f37454f6d1d8 Mon Sep 17 00:00:00 2001 From: Federico Tedin Date: Fri, 10 Jan 2025 11:55:46 +0100 Subject: [PATCH] Add XML endpoint --- pkg/http/http.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/pkg/http/http.go b/pkg/http/http.go index 9ede0fb..fd727c7 100644 --- a/pkg/http/http.go +++ b/pkg/http/http.go @@ -5,6 +5,7 @@ import ( "context" crand "crypto/rand" "encoding/json" + "encoding/xml" "errors" "fmt" "io" @@ -458,6 +459,35 @@ func (s *Server) WithHTTPTesting() *Server { w.WriteHeader(http.StatusOK) _, _ = w.Write(buf.Bytes()) }) + + r.Get("/api/xml", func(w http.ResponseWriter, r *http.Request) { + type param struct { + Key string `xml:"key"` + Value string `xml:"value"` + Index int `xml:"index"` + } + type response struct { + Params []param `xml:"params"` + } + data := []param{} + i := 0 + for key, value := range r.URL.Query() { + data = append(data, param{Key: key, Value: value[0], Index: i}) + i++ + } + + buf := bytes.Buffer{} + err := xml.NewEncoder(&buf).Encode(response{Params: data}) + 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/xml") + w.WriteHeader(http.StatusOK) + _, _ = w.Write(buf.Bytes()) + }) }) return s