response

package
v0.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 11, 2020 License: MIT Imports: 9 Imported by: 0

README

response GoDoc

Package response provides helpers and utils for working with HTTP response

Download:

go get -u github.com/vardius/go-api-boilerplate/pkg/http/response

Package response provides helpers and utils for working with HTTP response

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

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

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

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL