第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在代理請求時在 Go Gin 中設(shè)置新的請求正文

如何在代理請求時在 Go Gin 中設(shè)置新的請求正文

嚕嚕噠 2022-09-12 16:30:18
我正在嘗試代理 HTTP 調(diào)用,并嘗試在將 JSON 正文發(fā)送到代理服務(wù)之前對其進(jìn)行修改。但是,如果我嘗試使用任何新數(shù)據(jù)修改c.Request.Body,則POST請求以400錯誤格式結(jié)束。但是,如果我再次將相同的先前正文數(shù)據(jù)設(shè)置為 c.Request.Body,則代理調(diào)用可以正常工作。杜松子酒功能func Forward(c *gin.Context) {    remoteURL := c.Query("url")    remote, _ := url.Parse(remoteURL)    var bodyBytes []byte    if c.Request.Body != nil {        bodyBytes, _ = ioutil.ReadAll(c.Request.Body)    }    newBody := string(bodyBytes)    newBody = strings.Replace(newBody, "testString", "testString1", -1)    c.Request.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(newBody)))    proxy := httputil.NewSingleHostReverseProxy(remote)    proxy.Director = func(req *http.Request) {        req.Header = c.Request.Header        req.Host = remote.Host        req.URL.Scheme = remote.Scheme        req.URL.Host = remote.Host        req.URL.Path = remote.Path    }    proxy.ServeHTTP(c.Writer, c.Request)}卷曲命令:curl --location --request POST 'http://localhost:8020/v1/proxy?url=https://entgkdbyzqm27.x.pipedream.net/' \--header 'Content-Type: application/json' \--data-raw '{    "id": "testString"}'如果我能知道如何在Gin中正確設(shè)置請求正文,我將非常有義務(wù)。
查看完整描述

1 回答

?
慕工程0101907

TA貢獻(xiàn)1887條經(jīng)驗(yàn) 獲得超5個贊

您可能有不匹配的情況。替換后,新主體比前一個主體長。Content-Length


按如下方式編寫函數(shù):Director


    proxy.Director = func(req *http.Request) {

        // clone the headers

        req.Header = c.Request.Header.Clone()


        // 1. set new header

        req.Header.Set("Content-Length", strconv.Itoa(len(newBody)))


        // 2. also update this field

        req.ContentLength = int64(len(newBody))


        // the rest stays the same

        req.Host = remote.Host

        req.URL.Scheme = remote.Scheme

        req.URL.Host = remote.Host

        req.URL.Path = remote.Path

    }


查看完整回答
反對 回復(fù) 2022-09-12
  • 1 回答
  • 0 關(guān)注
  • 132 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號