json

package
v0.0.1-0...-1fb92f6 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2022 License: MIT Imports: 6 Imported by: 0

README

json 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/json

Package json 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

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func JSON

func JSON(ctx context.Context, w http.ResponseWriter, statusCode int, 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/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/vardius/go-api-boilerplate/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

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"

	apperrors "github.com/vardius/go-api-boilerplate/pkg/errors"
	"github.com/vardius/go-api-boilerplate/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/vardius/go-api-boilerplate/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/vardius/go-api-boilerplate/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/vardius/go-api-boilerplate/pkg/errors"
	"github.com/vardius/go-api-boilerplate/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

func NotAllowed() http.Handler

func NotFound

func NotFound() http.Handler

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

Jump to

Keyboard shortcuts

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