From b86e1dec64a7bdfb15e855abc13b9590e0c4b3a0 Mon Sep 17 00:00:00 2001 From: JustSong Date: Sun, 23 Apr 2023 19:19:43 +0800 Subject: [PATCH] Fix "HTTP decompression failed" --- common/constants.go | 2 +- controller/relay.go | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/common/constants.go b/common/constants.go index a5f1eb67..ef1f7031 100644 --- a/common/constants.go +++ b/common/constants.go @@ -108,7 +108,7 @@ const ( ChannelTypeOhMyGPT = 7 ) -var ChannelHosts = []string{ +var ChannelBaseURLs = []string{ "", // 0 "https://api.openai.com", // 1 "https://openai.api2d.net", // 2 diff --git a/controller/relay.go b/controller/relay.go index 6fe3e9ff..04e10e35 100644 --- a/controller/relay.go +++ b/controller/relay.go @@ -10,8 +10,8 @@ import ( func Relay(c *gin.Context) { channelType := c.GetInt("channel") - host := common.ChannelHosts[channelType] - req, err := http.NewRequest(c.Request.Method, fmt.Sprintf("%s%s", host, c.Request.URL.String()), c.Request.Body) + baseURL := common.ChannelBaseURLs[channelType] + req, err := http.NewRequest(c.Request.Method, fmt.Sprintf("%s%s", baseURL, c.Request.URL.String()), c.Request.Body) if err != nil { c.JSON(http.StatusOK, gin.H{ "error": gin.H{ @@ -22,6 +22,9 @@ func Relay(c *gin.Context) { return } req.Header = c.Request.Header.Clone() + // Fix HTTP Decompression failed + // https://github.com/stoplightio/prism/issues/1064#issuecomment-824682360 + req.Header.Del("Accept-Encoding") client := &http.Client{} resp, err := client.Do(req) @@ -38,8 +41,6 @@ func Relay(c *gin.Context) { c.Writer.Header().Set(k, v[0]) } _, err = io.Copy(c.Writer, resp.Body) - //body, err := io.ReadAll(resp.Body) - //_, err = c.Writer.Write(body) if err != nil { c.JSON(http.StatusOK, gin.H{ "error": gin.H{