Skip to content

Commit

Permalink
respond with JSON format
Browse files Browse the repository at this point in the history
Signed-off-by: viveksahu26 <vivekkumarsahu650@gmail.com>
  • Loading branch information
viveksahu26 committed Sep 14, 2022
1 parent ced7445 commit 1086ded
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/respondWithJSON.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package src

import (
"encoding/json"
"net/http"
)

// Responding with JSON format
func RespondWithJSON(w http.ResponseWriter, status int, resp interface{}) error {
response, err := json.Marshal(resp)
if err != nil {
return err
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
w.Write(response)
return nil
}

0 comments on commit 1086ded

Please sign in to comment.