gin_dump

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 24, 2025 License: MIT Imports: 11 Imported by: 0

README

gin-dump

Notice: This gin-dump is similar to sambar/gin-dump, but have a bit different to that one.

  • Gin middleware/handler to dump header/body of request and response.
  • Very helpful for debugging your applications.
  • More beautiful and compact than samber/gin-dump

Content-Type support

  • application/json
  • application/x-www-form-urlencoded

Usage

Download and install it / import it in your code:

$ go get github.com/AyakuraYuki/gin-dump
import "github.com/AyakuraYuki/gin-dump"
Examples:
package main

import (
	"fmt"

	"github.com/AyakuraYuki/gin-dump"
	"github.com/gin-gonic/gin"
)

func main() {
	router := gin.Default()

	// prints to stdout
	router.Use(gin_dump.Dump())

	// custom callback with default options
	router.Use(gin_dump.DumpFunc(func(dumpStr string) {
		fmt.Println(dumpStr)
	}))

	// dump with options and custom callback
	opts := []gin_dump.Option{
		gin_dump.WithShowReq(true),
		gin_dump.WithShowCookies(false),
		gin_dump.WithCallback(func(dumpStr string) {
			fmt.Println(dumpStr)
		}),
	}
	router.Use(gin_dump.DumpWithOptions(opts...))

	// ...
	router.Run()
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	StringMaxLength = 0
	Newline         = "\n"
	Indent          = 4
)

Functions

func Dump

func Dump() gin.HandlerFunc

Dump the requests and responses in os.Stdout

func DumpFunc

func DumpFunc(cb Callback) gin.HandlerFunc

DumpFunc dumps the requests and responses, passes the dumped information to your callback function, allowing you to consume the dumped information in your own way.

func DumpWithOptions

func DumpWithOptions(opts ...Option) gin.HandlerFunc

DumpWithOptions allows you to highly customize the middleware, such as dumping only requests or responses, dumping only the Header sections or body data within requests/responses, and using customized callback methods, among other configurations.

func FormatJsonBytes

func FormatJsonBytes(data []byte, hiddenFields []string, compactInArray bool) ([]byte, error)

func FormatToJson

func FormatToJson(v any, hiddenFields []string, compactInArray bool) ([]byte, error)

Types

type Callback

type Callback func(dumpStr string)

type Option

type Option func(opt *options)

func WithCallback

func WithCallback(cb Callback) Option

WithCallback accepts your customized callback method to which the dumped string information will be passed, allowing you to consume the dumped information in your own way.

func WithShowBody

func WithShowBody(show bool) Option

WithShowBody is set to true, it will dump the body data of requests or responses; when set to false, dumping will not occur.

However, if both ShowReq and ShowRsp are set to false, the body data of requests or responses will not be dumped regardless of the WithShowBody setting.

func WithShowCookies

func WithShowCookies(show bool) Option

WithShowCookies is set to true, it will keep cookies in header for the dumping; when set to false, cookies will not show in the dumped headers.

func WithShowHeaders

func WithShowHeaders(show bool) Option

WithShowHeaders is set to true, it will dump the header of requests or responses; when set to false, dumping will not occur.

However, if both ShowReq and ShowRsp are set to false, the header of requests or responses will not be dumped regardless of the WithShowHeaders settings.

func WithShowRaw

func WithShowRaw(show bool) Option

WithShowRaw is set to true, the raw format of the request body data will be preserved; when set to false, the dumped request body data will be formatted and sorted.

func WithShowReq

func WithShowReq(show bool) Option

WithShowReq is set to true, it will dump the header and body data of requests; when set to false, dumping will not occur.

func WithShowRsp

func WithShowRsp(show bool) Option

WithShowRsp is set to true, it will dump the header and body data of responses; when set to false, dumping will not occur.

Jump to

Keyboard shortcuts

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