golang复用http.request.body

栏目: Go · 发布时间: 7年前

内容简介:业务当中有需要分发http.request.body的场景。比如微信回调消息只能指定一个地址,所以期望可以复制一份消息发给其他服务。由服务B和接收微信回调的服务A一起处理微信回调信息。最开始考虑的是直接转发http.request。使用ReverseProxy直接将http.request由服务A转发给服务B。但是微信涉及到验证等问题,完全调整好非常麻烦。所以转换思路,打算将http.request.body的内容直接post给服务B。可是http.request是readcloser。我们将http.r

问题及场景

业务当中有需要分发http.request.body的场景。比如微信回调消息只能指定一个地址,所以期望可以复制一份消息发给其他服务。由服务B和接收微信回调的服务A一起处理微信回调信息。

解决思路

最开始考虑的是直接转发http.request。使用ReverseProxy直接将http.request由服务A转发给服务B。但是微信涉及到验证等问题,完全调整好非常麻烦。所以转换思路,打算将http.request.body的内容直接post给服务B。

可是http.request是readcloser。我们将http.request readAll的时候讲无法再次读取http.request里面的信息。

如何才能将http.request.body复制使用呢?

其中c表示的是http的上下文

// 把request的内容读取出来
    var bodyBytes []byte
    if c.Request.Body != nil {
        bodyBytes, _ = ioutil.ReadAll(c.Request.Body)
    }
    // 把刚刚读出来的再写进去
    c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))

1.我们先将body从http.request里面读取出来,保存到一个变量里面。

2.然后再将变量里面的数据使用ioutil.NopCloser方法写回到http.request里面。

https://golang.org/pkg/io/ioutil/#NopCloser

NopCloser returns a ReadCloser with a no-op Close method wrapping the provided Reader r.

NopCloser用一个无操作的Close方法包装Reader r返回一个ReadCloser接口。

这样我们就可以再次使用c.request来进行处理了。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Hit Refresh

Hit Refresh

Satya Nadella、Greg Shaw / HarperBusiness / 2017-9-26 / USD 20.37

Hit Refresh is about individual change, about the transformation happening inside of Microsoft and the technology that will soon impact all of our lives—the arrival of the most exciting and disruptive......一起来看看 《Hit Refresh》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换