github.com/amar224/phishing-tool@v0.9.0/controllers/api/response.go (about)

     1  package api
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	log "github.com/gophish/gophish/logger"
     9  )
    10  
    11  // JSONResponse attempts to set the status code, c, and marshal the given interface, d, into a response that
    12  // is written to the given ResponseWriter.
    13  func JSONResponse(w http.ResponseWriter, d interface{}, c int) {
    14  	dj, err := json.MarshalIndent(d, "", "  ")
    15  	if err != nil {
    16  		http.Error(w, "Error creating JSON response", http.StatusInternalServerError)
    17  		log.Error(err)
    18  	}
    19  	w.Header().Set("Content-Type", "application/json")
    20  	w.WriteHeader(c)
    21  	fmt.Fprintf(w, "%s", dj)
    22  }