Documentation ¶
Overview ¶
Package response provides helpers and utils for working with HTTP response
Package response provides helpers and utils for working with HTTP response ¶
Package response provides helpers and utils for working with HTTP response
Index ¶
- func Flush(w http.ResponseWriter)
- func JSON(ctx context.Context, w http.ResponseWriter, payload interface{}) error
- func JSONError(ctx context.Context, w http.ResponseWriter, err error) error
- func MustJSON(ctx context.Context, w http.ResponseWriter, payload interface{})
- func MustJSONError(ctx context.Context, w http.ResponseWriter, err error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Flush ¶
func Flush(w http.ResponseWriter)
Flush checks if it is stream response and sends any buffered data to the client.
func JSON ¶
func JSON(ctx context.Context, w http.ResponseWriter, payload interface{}) error
JSON returns data as json response
Example ¶
package main import ( "fmt" "net/http" "net/http/httptest" "github.com/vardius/go-api-boilerplate/pkg/http/response" ) func main() { type example struct { Name string `json:"name"` } h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) if err := response.JSON(r.Context(), w, example{"John"}); err != nil { panic(err) } }) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/", nil) h.ServeHTTP(w, req) fmt.Printf("%s\n", w.Body) }
Output: {"name":"John"}
Example (Second) ¶
package main import ( "fmt" "net/http" "net/http/httptest" "github.com/vardius/go-api-boilerplate/pkg/http/response" ) func main() { h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) if err := response.JSON(r.Context(), w, nil); err != nil { panic(err) } }) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/", nil) h.ServeHTTP(w, req) fmt.Printf("%s\n", w.Body) }
Output: {}
func JSONError ¶
JSONError returns data as json response uses JSON internally
Example ¶
package main import ( "fmt" "net/http" "net/http/httptest" "github.com/vardius/go-api-boilerplate/pkg/errors" "github.com/vardius/go-api-boilerplate/pkg/http/response" ) func main() { h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { appErr := errors.New("response error") if err := response.JSONError(r.Context(), w, appErr); err != nil { panic(err) } }) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/", nil) h.ServeHTTP(w, req) fmt.Printf("%s\n", w.Body) }
Output: {"code":500,"message":"Internal Server Error"}
func MustJSON ¶
func MustJSON(ctx context.Context, w http.ResponseWriter, 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/vardius/go-api-boilerplate/pkg/http/response" ) func main() { type example struct { Name string `json:"name"` } h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) response.MustJSON(r.Context(), w, example{"John"}) }) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/", nil) h.ServeHTTP(w, req) fmt.Printf("%s\n", w.Body) }
Output: {"name":"John"}
Example (Second) ¶
package main import ( "fmt" "net/http" "net/http/httptest" "github.com/vardius/go-api-boilerplate/pkg/http/response" ) func main() { h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) response.MustJSON(r.Context(), w, nil) }) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/", nil) h.ServeHTTP(w, req) fmt.Printf("%s\n", 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" "github.com/vardius/go-api-boilerplate/pkg/errors" "github.com/vardius/go-api-boilerplate/pkg/http/response" ) func main() { h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { appErr := errors.New("response error") response.MustJSONError(r.Context(), w, appErr) }) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/", nil) h.ServeHTTP(w, req) fmt.Printf("%s\n", w.Body) }
Output: {"code":500,"message":"Internal Server Error"}
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.