banner
biuaxia

biuaxia

"万物皆有裂痕,那是光进来的地方。"
github
bilibili
tg_channel

【Repost】Reusing http.request.body in golang

title: [Reprint] Reusing http.request.body in golang
date: 2021-07-06 11:14:22
comment: false
toc: true
category:

  • Golang
    tags:
  • Reprint
  • Go
  • golang
  • Reuse
  • http
  • request
  • body

This article is reproduced from: Reusing http.request.body in golang - Go Language Chinese Website - Golang Chinese Community


Issues and Scenarios#

There are scenarios in business where http.request.body needs to be distributed. For example, in the case of WeChat callback messages, only one address can be specified, so it is expected to be able to copy the message and send it to other services. Services B and A, which receive WeChat callback, process WeChat callback information together.

Solution Approach#

Initially, the idea was to directly forward http.request. Use ReverseProxy to directly forward http.request from service A to service B. However, WeChat involves verification and other issues, and it is very troublesome to adjust everything perfectly. Therefore, the train of thought was changed, and the plan was to directly post the content of http.request.body to service B.

However, http.request is a readcloser. When we readAll http.request, we will not be able to read the information inside http.request again.

How can we copy and use http.request.body?#

Here, c represents the http context.

    // Read the content of the request
    var bodyBytes []byte
    if c.Request.Body != nil {
        bodyBytes, _ = ioutil.ReadAll(c.Request.Body)
    }
    // Write the previously read content back in
    c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
  1. First, we read the body from http.request and save it to a variable.

  2. Then, we use the ioutil.NopCloser method to write the data from the variable back into 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 returns a ReadCloser interface with a Close method that does nothing, wrapping the provided Reader r.

This way, we can use c.request again for further processing.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.