Documentation ¶
Overview ¶
Package response provides helpers and utils for working with HTTP response
Package response provides helpers and utils for working with HTTP response
Index ¶
- func JSON(ctx context.Context, w http.ResponseWriter, statusCode int, ...) error
- func JSONError(ctx context.Context, w http.ResponseWriter, err error) error
- func MustJSON(ctx context.Context, w http.ResponseWriter, statusCode int, ...)
- func MustJSONError(ctx context.Context, w http.ResponseWriter, err error)
- func NotAllowed() http.Handler
- func NotFound() http.Handler
- type HandlerFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func JSON ¶
JSON returns data as json response
Example ¶
package main import ( "fmt" "net/http" "net/http/httptest" "github.com/bradishungry/go-api-learning/pkg/http/response/json" ) func main() { type example struct { Name string `json:"name"` } h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if err := json.JSON(r.Context(), w, http.StatusOK, example{"John"}); err != nil { panic(err) } }) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/", nil) h.ServeHTTP(w, req) fmt.Printf("%s", w.Body) }
Output: {"name":"John"}
Example (Second) ¶
package main import ( "fmt" "net/http" "net/http/httptest" "github.com/bradishungry/go-api-learning/pkg/http/response/json" ) func main() { h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if err := json.JSON(r.Context(), w, http.StatusOK, nil); err != nil { panic(err) } }) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/", nil) h.ServeHTTP(w, req) fmt.Printf("%s", w.Body) }
Output: {}
func JSONError ¶
JSONError returns data as json response uses JSON internally
Example ¶
package main import ( "fmt" "net/http" "net/http/httptest" apperrors "github.com/bradishungry/go-api-learning/pkg/errors" "github.com/bradishungry/go-api-learning/pkg/http/response/json" ) func main() { h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { appErr := apperrors.New("response error") if err := json.JSONError(r.Context(), w, appErr); err != nil { panic(err) } }) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/", nil) h.ServeHTTP(w, req) fmt.Printf("%s", w.Body) }
Output: {"code":500,"message":"Internal Server Error"}
func MustJSON ¶
func MustJSON(ctx context.Context, w http.ResponseWriter, statusCode int, payload interface{})
MustJSON returns data as json response will panic if unable to marshal payload into JSON object uses JSON internally
Example ¶
package main import ( "fmt" "net/http" "net/http/httptest" "github.com/bradishungry/go-api-learning/pkg/http/response/json" ) func main() { type example struct { Name string `json:"name"` } h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { json.MustJSON(r.Context(), w, http.StatusOK, example{"John"}) }) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/", nil) h.ServeHTTP(w, req) fmt.Printf("%s", w.Body) }
Output: {"name":"John"}
Example (Second) ¶
package main import ( "fmt" "net/http" "net/http/httptest" "github.com/bradishungry/go-api-learning/pkg/http/response/json" ) func main() { h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { json.MustJSON(r.Context(), w, http.StatusOK, nil) }) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/", nil) h.ServeHTTP(w, req) fmt.Printf("%s", w.Body) }
Output: {}
func MustJSONError ¶
func MustJSONError(ctx context.Context, w http.ResponseWriter, err error)
MustJSONError returns data as json response will panic if unable to marshal error into JSON object uses JSONError internally
Example ¶
package main import ( "fmt" "net/http" "net/http/httptest" apperrors "github.com/bradishungry/go-api-learning/pkg/errors" "github.com/bradishungry/go-api-learning/pkg/http/response/json" ) func main() { h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { appErr := apperrors.New("response error") json.MustJSONError(r.Context(), w, appErr) }) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/", nil) h.ServeHTTP(w, req) fmt.Printf("%s", w.Body) }
Output: {"code":500,"message":"Internal Server Error"}
func NotAllowed ¶
Types ¶
type HandlerFunc ¶
type HandlerFunc func(w http.ResponseWriter, r *http.Request) error
func (HandlerFunc) ServeHTTP ¶
func (f HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP calls f(w, r) and handles error
Click to show internal directories.
Click to hide internal directories.