@@ -13,7 +13,7 @@ import (
1313 log "github.com/sirupsen/logrus"
1414)
1515
16- func getJSONBody (data any , sanitizeJSON bool ) ([]byte , error ) {
16+ func getJSONBody (data any ) ([]byte , error ) {
1717 if data == nil {
1818 return nil , nil // Otherwise "null" (4 bytes) would be returned.
1919 }
@@ -22,20 +22,17 @@ func getJSONBody(data any, sanitizeJSON bool) ([]byte, error) {
2222 if err != nil {
2323 return nil , err
2424 }
25- if sanitizeJSON {
26- body = SanitizeJSONBytes (body )
27- if len (body ) == 2 && body [0 ] == '{' && body [1 ] == '}' {
28- return nil , nil
29- }
30- }
3125
3226 return body , nil
3327}
3428
35- // SendJSONResponse sends an HTTP response with an optionally sanitized JSON data.
29+ // SendJSONResponse sends an HTTP response with a JSON data.
3630// Caller may set additional headers like `w.Header().Set("Location", "https://me")` before calling this function.
37- func SendJSONResponse (w http.ResponseWriter , statusCode int , data any , sanitizeJSON bool ) (err error ) {
38- body , err := getJSONBody (data , sanitizeJSON )
31+ //
32+ // Warning: The last boolean parameter is ignored. It is there for temporary backward compatibility only.
33+ // It will be removed in the near-future.
34+ func SendJSONResponse (w http.ResponseWriter , statusCode int , data any , _ ... bool ) (err error ) {
35+ body , err := getJSONBody (data )
3936 if body != nil {
4037 w .Header ().Set (ContentTypeHeader , ContentTypeApplicationJSON )
4138 w .WriteHeader (statusCode )
@@ -46,7 +43,7 @@ func SendJSONResponse(w http.ResponseWriter, statusCode int, data any, sanitizeJ
4643 return err
4744}
4845
49- func sendResponse (w http.ResponseWriter , r * http.Request , data any , sanitizeJSON bool ) (err error ) {
46+ func sendResponse (w http.ResponseWriter , r * http.Request , data any ) (err error ) {
5047 okStatus := getOkStatus (w , r , data )
5148
5249 if data == nil {
@@ -74,13 +71,16 @@ func sendResponse(w http.ResponseWriter, r *http.Request, data any, sanitizeJSON
7471 return err
7572 }
7673
77- return SendJSONResponse (w , okStatus , data , sanitizeJSON )
74+ return SendJSONResponse (w , okStatus , data )
7875}
7976
8077// SendResponse sends an HTTP response with a JSON data.
8178// Caller may set additional headers like `w.Header().Set("Location", "https://me")` before calling this function.
79+ //
80+ // Same as SendJSONResponse. It will be removed in the near-future.
81+ // Deprecated.
8282func SendResponse (w http.ResponseWriter , statusCode int , data any ) error {
83- return SendJSONResponse (w , statusCode , data , false )
83+ return SendJSONResponse (w , statusCode , data )
8484}
8585
8686func getOkStatus (w http.ResponseWriter , r * http.Request , data any ) int {
@@ -95,19 +95,19 @@ func getOkStatus(w http.ResponseWriter, r *http.Request, data any) int {
9595}
9696
9797// SendResp sends an HTTP response with data.
98- // On no error 200/201/204 sent according to the request.
99- // On error send response depending on whether the error is created by NewError and the client supports RFC 7807.
98+ // If the supplied err is nil, 200/201/204 is sent according to the request and if response data is supplied .
99+ // If err is not nil, a response is sent depending on whether the supplied error is created by NewError and the client supports RFC 7807.
100100// Caller may set additional headers like `w.Header().Set("Location", "https://me")` before calling this function.
101101func SendResp (w http.ResponseWriter , r * http.Request , err error , data any ) error {
102102 if err == nil {
103- return sendResponse (w , r , data , LambdaSanitizeJSON )
103+ return sendResponse (w , r , data )
104104 }
105105
106106 if errStr := err .Error (); errStr != "" { // In some cases status like 404 does not indicate error, just a plain result. E.g. on a distributed cache query.
107107 log .Error (errStr )
108108 }
109109
110- body , _ := getJSONBody (data , LambdaSanitizeJSON )
110+ body , _ := getJSONBody (data )
111111 if body == nil {
112112 return SendProblemDetails (w , r , err )
113113 }
@@ -124,6 +124,11 @@ func SendEmptyResponse(w http.ResponseWriter, statusCode int) {
124124}
125125
126126// SendLocationResponse sends an empty "201 Created" HTTP response with Location header.
127+ //
128+ // Looking at the APIs of various bodies, it seems that 201 Created responses usually send back the JSON description of the created resource.
129+ // So, this function is practically never used. Thus, it will be removed in the near-future.
130+ // One can use SendEmptyResponse instead.
131+ // Deprecated.
127132func SendLocationResponse (w http.ResponseWriter , location string ) {
128133 w .Header ().Set ("Location" , location )
129134 SendEmptyResponse (w , http .StatusCreated )
@@ -201,7 +206,7 @@ func sendCustomResponse(r *http.Request, w http.ResponseWriter, body []byte, sta
201206 return nil
202207}
203208
204- // SendProblemDetails adds detailed problem description to JSON body, if available. See RFC 7807.
209+ // SendProblemDetails sends a response adding detailed problem description to JSON body, if available. See RFC 7807.
205210func SendProblemDetails (w http.ResponseWriter , r * http.Request , err error ) error {
206211 if restErr , ok := err .(* restError ); ok {
207212 if len (restErr .body ) != 0 {
0 commit comments