Skip to content

Commit

Permalink
Merge pull request #3 from tgmpje/jsonsupport
Browse files Browse the repository at this point in the history
Added ServeJSON() to serve JSON content
  • Loading branch information
Jille committed Sep 6, 2022
2 parents e432ef6 + 4ccb624 commit aac8046
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions respond/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package respond

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

"github.com/Jille/convreq/internal"
)

type respondJSON struct {
data interface{}
}

// Respond implements convreq.HttpResponse.
func (rj respondJSON) Respond(w http.ResponseWriter, r *http.Request) error {
if w.Header().Get("Content-Type") == "" {
w.Header().Set("Content-Type", "application/json")
}
return json.NewEncoder(w).Encode(rj.data)
}

// ServeJSON uses json.Marshal() to serve a JSON object.
func ServeJSON(data interface{}) internal.HttpResponse {
return respondJSON{data}
}

0 comments on commit aac8046

Please sign in to comment.