func Respond(w http.ResponseWriter, r *http.Request, code int, data interface{}) {

	var jsonData []byte
	var err error

	// If we have data to respond with, encode it into JSON, and set the correct
	// header. If we cannot encode, we'll return an Internal Server Error.
	if data != nil {
		// Set the content-type if not already set
		if w.Header().Get("Content-Type") == "" {
			w.Header().Set("Content-Type", "application/json")
		}

		// Marshal data into byte array
		jsonData, err = json.Marshal(data)
		if err != nil {
			// There was an error Marshalling, so return a server error
			Error(w, r, "Internal Server Error", http.StatusInternalServerError)
			return
		}
	}

	// Set status code value on request details so other middlewares can access it
	if d := getDetails(r); d != nil {
		d.StatusCode = code
	}

	// Set the status code of the response. This should be the last header to be written.
	w.WriteHeader(code)

	// Write the JSON body. This must be done last, otherwise we flush the response too quickly.
	if len(jsonData) > 0 {
		//nolint:errcheck
		w.Write(jsonData)
	}
}
// Respond should be used to respond to a http request within a http handler.
// Respond encodes any data passed in as JSON.
// Respond also sets the status code of the response on the request details, so
// middlewares can access this value.
Respond should be used to respond to a http request within a http handler.
Respond encodes any data passed in as JSON.
Respond also sets the status code of the response on the request details, so
middlewares can access this value.
// Respond should be used to respond to a http request within a http handler . // Respond encodes any data passed in as JSON . // Respond also sets the status code of the response on the request details so // middlewares can access this value .
func Respond(w http.ResponseWriter, r *http.Request, code int, data interface{}) {
	var jsonData []byte
	var err error
	if data != nil {
		if w.Header().Get("Content-Type") == "" {
			w.Header().Set("Content-Type", "application/json")
		}
		jsonData, err = json.Marshal(data)
		if err != nil {
			Error(w, r, "Internal Server Error", http.StatusInternalServerError)
			return
		}
	}
	if d := getDetails(r); d != nil {
		d.StatusCode = code
	}
	w.WriteHeader(code)
	if len(jsonData) > 0 {
		w.Write(jsonData)
	}
}
func Respond ( w http . ResponseWriter , r * http . Request , code int , data interface { } ) { var jsonData [ ] byte var err error if data != nil { if w . Header ( ) . Get ( " " ) == " " { w . Header ( ) . Set ( " " , " " ) } jsonData , err = json . Marshal ( data ) if err != nil { Error ( w , r , " " , http . StatusInternalServerError ) return } } if d := getDetails ( r ) ; d != nil { d . StatusCode = code } w . WriteHeader ( code ) if len ( jsonData ) > 0 { w . Write ( jsonData ) } }
Respond should be used to respond to a http request within a http handler.
Respond should be used to respond to a http request within a http handler .
// If we have data to respond with, encode it into JSON, and set the correct // header. If we cannot encode, we'll return an Internal Server Error. // Set the content-type if not already set // Marshal data into byte array // There was an error Marshalling, so return a server error // Set status code value on request details so other middlewares can access it // Set the status code of the response. This should be the last header to be written. // Write the JSON body. This must be done last, otherwise we flush the response too quickly. //nolint:errcheck