Skip to content

Commit

Permalink
Add XML endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
federicotdn committed Jan 10, 2025
1 parent 06175ad commit 614b7b4
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions pkg/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
crand "crypto/rand"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 614b7b4

Please sign in to comment.