go-api-boilerplate: github.com/vardius/go-api-boilerplate/pkg/http/response Index | Examples | Files

package response

import "github.com/vardius/go-api-boilerplate/pkg/http/response"

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

Examples

Package Files

json.go json_error.go response.go

func Flush Uses

func Flush(w http.ResponseWriter)

Flush checks if it is stream response and sends any buffered data to the client.

func JSON Uses

func JSON(ctx context.Context, w http.ResponseWriter, payload interface{}) error

JSON returns data as json response

Code:

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"}

Code:

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 Uses

func JSONError(ctx context.Context, w http.ResponseWriter, err error) error

JSONError returns data as json response uses JSON internally

Code:

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 Uses

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

Code:

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"}

Code:

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 Uses

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

Code:

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"}

Package response imports 9 packages (graph) and is imported by 5 packages. Updated 2020-08-04. Refresh now. Tools for package owners.