Просмотр исходного кода

Fix "HTTP decompression failed"

JustSong 2 лет назад
Родитель
Сommit
b86e1dec64
2 измененных файлов с 6 добавлено и 5 удалено
  1. 1 1
      common/constants.go
  2. 5 4
      controller/relay.go

+ 1 - 1
common/constants.go

@@ -108,7 +108,7 @@ const (
 	ChannelTypeOhMyGPT   = 7
 	ChannelTypeOhMyGPT   = 7
 )
 )
 
 
-var ChannelHosts = []string{
+var ChannelBaseURLs = []string{
 	"",                            // 0
 	"",                            // 0
 	"https://api.openai.com",      // 1
 	"https://api.openai.com",      // 1
 	"https://openai.api2d.net",    // 2
 	"https://openai.api2d.net",    // 2

+ 5 - 4
controller/relay.go

@@ -10,8 +10,8 @@ import (
 
 
 func Relay(c *gin.Context) {
 func Relay(c *gin.Context) {
 	channelType := c.GetInt("channel")
 	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 {
 	if err != nil {
 		c.JSON(http.StatusOK, gin.H{
 		c.JSON(http.StatusOK, gin.H{
 			"error": gin.H{
 			"error": gin.H{
@@ -22,6 +22,9 @@ func Relay(c *gin.Context) {
 		return
 		return
 	}
 	}
 	req.Header = c.Request.Header.Clone()
 	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{}
 	client := &http.Client{}
 
 
 	resp, err := client.Do(req)
 	resp, err := client.Do(req)
@@ -38,8 +41,6 @@ func Relay(c *gin.Context) {
 		c.Writer.Header().Set(k, v[0])
 		c.Writer.Header().Set(k, v[0])
 	}
 	}
 	_, err = io.Copy(c.Writer, resp.Body)
 	_, err = io.Copy(c.Writer, resp.Body)
-	//body, err := io.ReadAll(resp.Body)
-	//_, err = c.Writer.Write(body)
 	if err != nil {
 	if err != nil {
 		c.JSON(http.StatusOK, gin.H{
 		c.JSON(http.StatusOK, gin.H{
 			"error": gin.H{
 			"error": gin.H{