日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

兩種方法實現 Http Request Body 多次讀取

來源: 責編: 時間:2024-01-02 17:28:21 215觀看
導讀大家好, 我是 老麥, 一個運維老兵, 現在專注于 Golang,DevOps,云原生基礎設施建設。原文鏈接: https://typonotes.com/posts/2024/01/02/http-request-multiple-times-read/最近在使用 gin 的時候, 踩了一個重復讀取的 Requ

大家好, 我是 老麥, 一個運維老兵, 現在專注于 Golang,DevOps,云原生基礎設施建設。2G228資訊網——每日最新資訊28at.com

原文鏈接: https://typonotes.com/posts/2024/01/02/http-request-multiple-times-read/2G228資訊網——每日最新資訊28at.com

最近在使用 gin 的時候, 踩了一個重復讀取的 Request.Body 的坑。2G228資訊網——每日最新資訊28at.com

起因是 gin 的 gin.Context{} 提供了 c.Copy() 方法創建副本。這個方法一直在用, 但不知道從什么時候開始, 一直認為這個方法是 深拷貝, 但 并不完全是 (T_T)2G228資訊網——每日最新資訊28at.com

// Copy returns a copy of the current context that can be safely used outside the request's scope.// This has to be used when the context has to be passed to a goroutine.func (c *Context) Copy() *Context { cp := Context{  writermem: c.writermem,  Request:   c.Request, // 指針, 也算引用類型。 沒有實現完全復制  Params:    c.Params,  engine:    c.engine, } cp.writermem.ResponseWriter = nil cp.Writer = &cp.writermem cp.index = abortIndex cp.handlers = nil cp.Keys = map[string]interface{}{} // Keys 完全復制 for k, v := range c.Keys {  cp.Keys[k] = v } paramCopy := make([]Param, len(cp.Params)) // 切片, 完全復制 copy(paramCopy, cp.Params)  cp.Params = paramCopy return &cp}

1. gin 通過用一個全局變量保存

在 gin 中, 在讀取了 request body 后, 通過 c.Set(BodyBytesKey, body) 放到了 gin.Context 中的 Keys。這是一個 map, 上面說到了。2G228資訊網——每日最新資訊28at.com

因此 在 gin 中通過中間變量實現類似效果。雖然感覺上多次讀取 Body , 但實際 只讀取了一次,2G228資訊網——每日最新資訊28at.com

// ShouldBindBodyWith is similar with ShouldBindWith, but it stores the request// body into the context, and reuse when it is called again.//// NOTE: This method reads the body before binding. So you should use// ShouldBindWith for better performance if you need to call only once.func (c *Context) ShouldBindBodyWith(obj any, bb binding.BindingBody) (err error) { var body []byte if cb, ok := c.Get(BodyBytesKey); ok {  if cbb, ok := cb.([]byte); ok {   body = cbb  } } if body == nil {  body, err = io.ReadAll(c.Request.Body)  if err != nil {   return err  }  // 將 Body 中的內容放到 gin.Context 中的 Keys 中  c.Set(BodyBytesKey, body) } return bb.BindBody(body, obj)}

參考文檔: https://github.com/gin-gonic/gin/blob/v1.9.1/context.go#L744-L7642G228資訊網——每日最新資訊28at.com

2. 再造一個 Request

另外一種方法, 就是在讀取 Body 后, 重建一個 Requset 再把 Body 放進去。2G228資訊網——每日最新資訊28at.com

// 讀取老的body, err := ioutil.ReadAll(r.Body)if err != nil {    // ...}url, _ := url.Parse(config.GetGameHost())// 創建新的r2 := r.Clone(r.Context())// 將數據方進去r.Body = ioutil.NopCloser(bytes.NewReader(body))r2.Body = ioutil.NopCloser(bytes.NewReader(body))r.ParseForm()proxy := httputil.NewSingleHostReverseProxy(url)proxy.ServeHTTP(w, r2)

參考文檔: https://stackoverflow.com/q/620171462G228資訊網——每日最新資訊28at.com

注意 http.Request 有一個方法叫 Clone(), 但這也不是一個完全的深拷貝。Body 沒有復制。2G228資訊網——每日最新資訊28at.com

// Clone returns a deep copy of r with its context changed to ctx.// The provided ctx must be non-nil.//// For an outgoing client request, the context controls the entire// lifetime of a request and its response: obtaining a connection,// sending the request, and reading the response headers and body.func (r *Request) Clone(ctx context.Context) *Request { if ctx == nil {  panic("nil context") } r2 := new(Request) *r2 = *r r2.ctx = ctx r2.URL = cloneURL(r.URL) if r.Header != nil {  r2.Header = r.Header.Clone() } if r.Trailer != nil {  r2.Trailer = r.Trailer.Clone() } if s := r.TransferEncoding; s != nil {  s2 := make([]string, len(s))  copy(s2, s)  r2.TransferEncoding = s2 } r2.Form = cloneURLValues(r.Form) r2.PostForm = cloneURLValues(r.PostForm) r2.MultipartForm = cloneMultipartForm(r.MultipartForm) return r2}

本文鏈接:http://www.www897cc.com/showinfo-26-56416-0.html兩種方法實現 Http Request Body 多次讀取

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: ExecutorCompletionService詳解,你學會了嗎?

下一篇: 2023 年十種最佳用戶體驗交互設計

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 通城县| 十堰市| 肃南| 宣威市| 车致| 木里| 双牌县| 乐山市| 醴陵市| 安化县| 青神县| 澄迈县| 朝阳区| 青川县| 朔州市| 庆云县| 赤峰市| 四川省| 南木林县| 静安区| 商水县| 辰溪县| 弋阳县| 富宁县| 海原县| 亚东县| 西乌珠穆沁旗| 虞城县| 潮安县| 西丰县| 台中市| 潼关县| 莒南县| 鄂伦春自治旗| 大田县| 根河市| 广宁县| 威信县| 揭东县| 柞水县| 河东区|