forked from openshift/osin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response_json.go
47 lines (43 loc) · 1.15 KB
/
response_json.go
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
37
38
39
40
41
42
43
44
45
46
47
package osin
import (
"encoding/json"
// "net/http"
"github.com/valyala/fasthttp"
)
// OutputJSON encodes the Response to JSON and writes to the http.ResponseWriter
func OutputJSON(rs *Response, r *fasthttp.RequestCtx) error {
// Add headers
for i, k := range rs.Headers {
for _, v := range k {
r.Response.Header.Add(i, v)
// w.Header().Add(i, v)
}
}
if rs.Type == REDIRECT {
// Output redirect with parameters
u, err := rs.GetRedirectUrl()
if err != nil {
return err
}
r.Response.Header.Add("Location", u)
r.Response.Header.SetStatusCode(302)
//w.Header().Add("Location", u)
//w.WriteHeader(302)
} else {
// set content type if the response doesn't already have one associated with it
if r.Response.Header.Peek("Content-Type") == nil {
r.Response.Header.Set("Content-Type", "application/json")
}
// if w.Header().Get("Content-Type") == "" {
// w.Header().Set("Content-Type", "application/json")
// }
// w.WriteHeader(rs.StatusCode)
r.Response.Header.SetStatusCode(rs.StatusCode)
encoder := json.NewEncoder(r.Response.BodyWriter())
err := encoder.Encode(rs.Output)
if err != nil {
return err
}
}
return nil
}