设为首页 加入收藏

TOP

gin自定义中间件解决requestBody不可重复读问题
2023-07-23 13:28:57 】 浏览:24
Tags:gin 解决 requestBody

先直接上代码

	r := gin.Default()
	// 注册中间件,使body可以重复读取
	r.Use(func(context *gin.Context) {
		all, err := context.GetRawData() // 读取body的内容
		if err != nil {
			log.Fatal(err)
		}
		// 重写 GetBody 方法,以便后续的其他操作
		context.Request.GetBody = func() (io.ReadCloser, error) {
			context.Request.Body = io.NopCloser(bytes.NewBuffer(all))
			buffer := bytes.NewBuffer(all)
			closer := io.NopCloser(buffer)
			return closer, nil
		}
		body, _ := context.Request.GetBody()  // 每次调用GetBody方法,都会新生成一个io.ReadCloser,但是底层的byte数据,都是all变量缓存的。
		context.Request.Body = body
		context.Next()

	})

注意,上面的中间件,需要在第一个执行。

分析

在gin中,context.Request.Body 是一个io.ReadCloser的接口,如下图
image

查看io.ReadCloser接口定义

type ReadCloser interface {
	Reader
	Closer
}

type Reader interface {
	Read(p []byte) (n int, err error)
}

type Closer interface {
	Close() error
}

我们发现io.ReaderCloser接口的本质就是Read(p []byte) (n int, err error)Close() error 的组合。

所以我们只需要自己编写实现Read(p []byte) (n int, err error)Close() error 这两个方法的结构体即可赋值给context.Request.Body,在我们自己实现的方法中实现可重复读取即可达到我们的目的。

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Golang GMP原理(1) 下一篇go slice使用

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目