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

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

轉(zhuǎn)到 http,使用 client.Do 將傳入的 http.request 發(fā)送到其他服務(wù)器

轉(zhuǎn)到 http,使用 client.Do 將傳入的 http.request 發(fā)送到其他服務(wù)器

Go
慕桂英546537 2021-12-07 10:03:50
這是我的用例我們有一個服務(wù)“foobar”,它有兩個版本legacy 和version_2_of_doom (都在運行中)為了進行從legacy到的轉(zhuǎn)換version_2_of_doom,我們希望第一次將兩個版本放在一起,并在兩個版本上都收到 POST 請求(因為這里只有一個 POST api 調(diào)用)。我看到如何去做的方式。將是修改legacy 處理程序開頭的代碼,以便將請求復(fù)制到version_2_of_doom func(w http.ResponseWriter, req *http.Request) {     req.URL.Host = "v2ofdoom.local:8081"     req.Host = "v2ofdoom.local:8081"     client := &http.Client{}     client.Do(req)     // legacy code 但似乎沒有這么簡單它失敗了 http: Request.RequestURI can't be set in client requests.是否有一種眾所周知的方法來執(zhí)行這種操作(即在不接觸的情況下傳輸)ahttp.Request到其他服務(wù)器?
查看完整描述

3 回答

?
慕妹3146593

TA貢獻1820條經(jīng)驗 獲得超9個贊

您需要將所需的值復(fù)制到新請求中。由于這與反向代理的作用非常相似,您可能需要查看“net/http/httputil”對ReverseProxy.


創(chuàng)建一個新請求,并僅復(fù)制要發(fā)送到下一個服務(wù)器的請求部分。如果您打算在兩個地方使用它,您還需要讀取和緩沖請求正文:


func handler(w http.ResponseWriter, req *http.Request) {

    // we need to buffer the body if we want to read it here and send it

    // in the request. 

    body, err := ioutil.ReadAll(req.Body)

    if err != nil {

        http.Error(w, err.Error(), http.StatusInternalServerError)

        return

    }


    // you can reassign the body if you need to parse it as multipart

    req.Body = ioutil.NopCloser(bytes.NewReader(body))


    // create a new url from the raw RequestURI sent by the client

    url := fmt.Sprintf("%s://%s%s", proxyScheme, proxyHost, req.RequestURI)


    proxyReq, err := http.NewRequest(req.Method, url, bytes.NewReader(body))


    // We may want to filter some headers, otherwise we could just use a shallow copy

    // proxyReq.Header = req.Header

    proxyReq.Header = make(http.Header)

    for h, val := range req.Header {

        proxyReq.Header[h] = val

    }


    resp, err := httpClient.Do(proxyReq)

    if err != nil {

        http.Error(w, err.Error(), http.StatusBadGateway)

        return

    }

    defer resp.Body.Close()


    // legacy code

}


查看完整回答
反對 回復(fù) 2021-12-07
?
狐的傳說

TA貢獻1804條經(jīng)驗 獲得超3個贊

根據(jù)我的經(jīng)驗,實現(xiàn)這一點的最簡單方法是簡單地創(chuàng)建一個新請求并將您需要的所有請求屬性復(fù)制到新的請求對象中:


func(rw http.ResponseWriter, req *http.Request) {

    url := req.URL

    url.Host = "v2ofdoom.local:8081"


    proxyReq, err := http.NewRequest(req.Method, url.String(), req.Body)

    if err != nil {

        // handle error

    }


    proxyReq.Header.Set("Host", req.Host)

    proxyReq.Header.Set("X-Forwarded-For", req.RemoteAddr)


    for header, values := range req.Header {

        for _, value := range values {

            proxyReq.Header.Add(header, value)

        }

    }


    client := &http.Client{}

    proxyRes, err := client.Do(proxyReq)


    // and so on...

這種方法的好處是不修改原始請求對象(也許您的處理程序函數(shù)或存在于堆棧中的任何中間件函數(shù)仍然需要原始對象?)。


查看完整回答
反對 回復(fù) 2021-12-07
?
明月笑刀無情

TA貢獻1828條經(jīng)驗 獲得超4個贊

使用原始請求(僅在原始請求仍需要時才復(fù)制或復(fù)制):


func handler(w http.ResponseWriter, r *http.Request) {

    // Step 1: rewrite URL

    URL, _ := url.Parse("https://full_generic_url:123/x/y")

    r.URL.Scheme = URL.Scheme

    r.URL.Host = URL.Host

    r.URL.Path = singleJoiningSlash(URL.Path, r.URL.Path)

    r.RequestURI = ""


    // Step 2: adjust Header

    r.Header.Set("X-Forwarded-For", r.RemoteAddr)


    // note: client should be created outside the current handler()

    client := &http.Client{} 

    // Step 3: execute request

    resp, err := client.Do(r)

    if err != nil {

        http.Error(w, err.Error(), http.StatusInternalServerError)

        return

    }


    // Step 4: copy payload to response writer

    copyHeader(w.Header(), resp.Header)

    w.WriteHeader(resp.StatusCode)

    io.Copy(w, resp.Body)

    resp.Body.Close()

}


// copyHeader and singleJoiningSlash are copy from "/net/http/httputil/reverseproxy.go"

func copyHeader(dst, src http.Header) {

    for k, vv := range src {

        for _, v := range vv {

            dst.Add(k, v)

        }

    }

}


func singleJoiningSlash(a, b string) string {

    aslash := strings.HasSuffix(a, "/")

    bslash := strings.HasPrefix(b, "/")

    switch {

    case aslash && bslash:

        return a + b[1:]

    case !aslash && !bslash:

        return a + "/" + b

    }

    return a + b

}


查看完整回答
反對 回復(fù) 2021-12-07
  • 3 回答
  • 0 關(guān)注
  • 237 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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