File tree 2 files changed +27
-9
lines changed
2 files changed +27
-9
lines changed Original file line number Diff line number Diff line change @@ -53,12 +53,18 @@ func (d *DefaultEncoder) Encode(
53
53
)
54
54
}
55
55
56
+ // Set the Content-Type header if it hasn't been set already
57
+ if w .Header ().Get ("Content-Type" ) == "" {
58
+ w .Header ().Set ("Content-Type" , "application/json" )
59
+ }
60
+
61
+ // Write the HTTP status if it hasn't been written already
62
+ if w .Header ().Get ("X-Status-Written" ) == "" {
63
+ w .Header ().Set ("X-Status-Written" , "true" )
64
+ w .WriteHeader (httpStatus )
65
+ }
66
+
56
67
// Write the JSON body to the response
57
- w .WriteHeader (httpStatus )
58
- w .Header ().Set ("Content-Type" , "application/json" )
59
68
_ , err = w .Write (jsonBody )
60
- if err != nil {
61
- return err
62
- }
63
- return nil
69
+ return err
64
70
}
Original file line number Diff line number Diff line change @@ -31,8 +31,23 @@ func (d *DefaultStreamEncoder) Encode(
31
31
body := response .Body (d .mode )
32
32
httpStatus := response .HTTPStatus ()
33
33
34
+ // Set the Content-Type header if it hasn't been set already
35
+ if w .Header ().Get ("Content-Type" ) == "" {
36
+ w .Header ().Set ("Content-Type" , "application/json" )
37
+ }
38
+
39
+ // Write the HTTP status if it hasn't been written already
40
+ if w .Header ().Get ("X-Status-Written" ) == "" {
41
+ w .Header ().Set ("X-Status-Written" , "true" )
42
+ w .WriteHeader (httpStatus )
43
+ }
44
+
34
45
// Encode the JSON body
35
46
if err = json .NewEncoder (w ).Encode (body ); err != nil {
47
+ // Overwrite the status on error
48
+ w .Header ().Set ("X-Status-Written" , "true" )
49
+ w .WriteHeader (http .StatusInternalServerError )
50
+
36
51
_ = d .Encode (
37
52
w ,
38
53
gonethttpstatusresponse .NewJSendDebugInternalServerError (
@@ -42,8 +57,5 @@ func (d *DefaultStreamEncoder) Encode(
42
57
)
43
58
return err
44
59
}
45
- w .Header ().Set ("Content-Type" , "application/json" )
46
- w .WriteHeader (httpStatus )
47
-
48
60
return nil
49
61
}
You can’t perform that action at this time.
0 commit comments