-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathweb.go
More file actions
36 lines (33 loc) · 948 Bytes
/
web.go
File metadata and controls
36 lines (33 loc) · 948 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package problems
import (
"encoding/json"
"encoding/xml"
"net/http"
)
// ProblemHandler returns a http.HandlerFunc which writes a provided problem
// to a http.ResponseWriter as JSON with the status code.
func ProblemHandler(p *Problem) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", ProblemMediaType)
if p.Status != 0 {
w.WriteHeader(p.Status)
}
_ = json.NewEncoder(w).Encode(p)
}
}
// XMLProblemHandler returns a http.HandlerFunc which writes a provided problem
// to a http.ResponseWriter as XML with the status code.
func XMLProblemHandler(p *Problem) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", ProblemMediaTypeXML)
if p.Status != 0 {
w.WriteHeader(p.Status)
}
_ = xml.NewEncoder(w).Encode(struct {
XMLName xml.Name `xml:"urn:ietf:rfc:7807 problem"`
Problem
}{
Problem: *p,
})
}
}