Skip to content

Commit

Permalink
Merge pull request #4 from codimite/request-utils-version-1
Browse files Browse the repository at this point in the history
request utils add
  • Loading branch information
savina-weerasooriya-codimite authored May 25, 2023
2 parents aca956e + a755e6a commit 71831b3
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package gomite

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

func ParseJson(req *http.Request, dst interface{}) (err error) {
err = json.NewDecoder(req.Body).Decode(dst)
return
}

func SendJson(rw http.ResponseWriter, data interface{}, status ...int) {
if len(status) == 0 {
status = []int{http.StatusOK}
}
rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(status[0])
json.NewEncoder(rw).Encode(data)
return
}

func GetQueryValue(req *http.Request, key string) string {
return req.URL.Query().Get(key)
}

func GetHeaderValue(req *http.Request, key string) string {
return req.Header.Get(key)
}

0 comments on commit 71831b3

Please sign in to comment.