@@ -5,10 +5,13 @@ import (
55 "net/http"
66)
77
8+ // ContentType is the standard media type for Problem Details over HTTP.
9+ const ContentType = "application/problem+json; charset=utf-8"
10+
811// Write automatically writes the Problem Details as a JSON HTTP response
912// with the correct Application/Problem+JSON content type.
1013func (p * Problem ) Write (w http.ResponseWriter ) error {
11- w .Header ().Set ("Content-Type" , "application/problem+json; charset=utf-8" )
14+ w .Header ().Set ("Content-Type" , ContentType )
1215 w .Header ().Set ("X-Content-Type-Options" , "nosniff" )
1316 w .WriteHeader (p .Status )
1417
@@ -22,6 +25,31 @@ func (p *Problem) Write(w http.ResponseWriter) error {
2225 return nil
2326}
2427
25- // Handler is a generic middleware interface or handler concept, but realistically
26- // you just need `problem.Write(w)`.
27- // We can provide a functional wrapper if you wanted, but usually `Write` is enough.
28+ // JSON returns the JSON encoding of the Problem.
29+ // This is useful for frameworks like Gin, Echo, or Fiber where you might
30+ // want to write the raw JSON bytes directly and set the Content-Type manually.
31+ func (p * Problem ) JSON () []byte {
32+ b , _ := json .Marshal (p )
33+ return b
34+ }
35+
36+ // Headers returns a map of HTTP headers that should be set when returning this problem.
37+ // This is convenient for translating to other framework's header structures.
38+ func (p * Problem ) Headers () map [string ]string {
39+ return map [string ]string {
40+ "Content-Type" : ContentType ,
41+ "X-Content-Type-Options" : "nosniff" ,
42+ }
43+ }
44+
45+ // Map converts the Problem to a generic map map[string]any.
46+ // This is particularly useful when returning Problem Details via frameworks
47+ // that prefer or require maps for custom JSON serialization overrides.
48+ func (p * Problem ) Map () map [string ]any {
49+ var m map [string ]any
50+ // Reusing Marshal/Unmarshal avoids duplicating MarshalJSON logic
51+ // and preserves consistency with the struct's existing serialization.
52+ b , _ := json .Marshal (p )
53+ _ = json .Unmarshal (b , & m )
54+ return m
55+ }
0 commit comments