如何在Golang Gin框架中返回gzip压缩的响应。

huangapple go评论97阅读模式
英文:

How to return gzip response for golang gin framework

问题

我正在尝试使用Golang框架gin返回gzip响应。

他们在这里提供了一个示例:
https://github.com/gin-gonic/contrib/blob/master/gzip/example/example.go

package main

import (
	"fmt"
	"github.com/gin-gonic/contrib/gzip"
	"github.com/gin-gonic/gin"
	"time"
)

func main() {
	r := gin.Default()
	r.Use(gzip.Gzip(gzip.DefaultCompression))
	r.GET("/ping", func(c *gin.Context) {
		c.String(200, "pong "+fmt.Sprint(time.Now().Unix()))
	})

	// Listen and Server in 0.0.0.0:8080
	r.Run(":8080")
}

当我使用这个示例时,它没有返回gzip内容。

使用上面的代码,我从curl命令得到以下输出:

curl -v -H "Accept-Encoding: gzip" 'http://localhost:8080/ping'

Trying ::1...
Connected to localhost (::1) port 8080 (#0) GET /ping HTTP/1.1
Host: localhost:8080
User-Agent: curl/7.43.0
Accept: /
Accept-Encoding: gzip

< HTTP/1.1 200 OK
< Content-Encoding: gzip
< Content-Type: text/plain; charset=utf-8
< Vary: Accept-Encoding
< Date: Mon, 18 Jul 2016 17:20:56 GMT
< Content-Length: 38
<
Connection #0 to host localhost left intact pong 1468862456?n????

注意到内容没有被gzip压缩,并且添加了额外的乱码字符 "?n????"。

到目前为止,我无法弄清楚如何使其返回gzip。我认为示例代码中有一个错误,但我很难找出来。

提前谢谢你。

英文:

I'm trying to return a gzip response using the golang framework gin.

They provide an example here:
https://github.com/gin-gonic/contrib/blob/master/gzip/example/example.go

package main

import (
	&quot;fmt&quot;
	&quot;github.com/gin-gonic/contrib/gzip&quot;
	&quot;github.com/gin-gonic/gin&quot;
	&quot;time&quot;
)

func main() {
	r := gin.Default()
	r.Use(gzip.Gzip(gzip.DefaultCompression))
	r.GET(&quot;/ping&quot;, func(c *gin.Context) {
		c.String(200, &quot;pong &quot;+fmt.Sprint(time.Now().Unix()))
	})

	// Listen and Server in 0.0.0.0:8080
	r.Run(&quot;:8080&quot;)
}

When I use this example its not returning gzipped content.

With the code above I get the following output from curl

curl -v -H &quot;Accept-Encoding: gzip&quot; &#39;http://localhost:8080/ping&#39;

Trying ::1...
Connected to localhost (::1) port 8080 (#0) GET /ping HTTP/1.1 Host: localhost:8080 User-Agent: curl/7.43.0 Accept: / Accept-Encoding: gzip
&lt; HTTP/1.1 200 OK
&lt; Content-Encoding: gzip
&lt; Content-Type: text/plain; charset=utf-8
&lt; Vary: Accept-Encoding
&lt; Date: Mon, 18 Jul 2016 17:20:56 GMT
&lt; Content-Length: 38
&lt;

Connection #0 to host localhost left intact pong 1468862456?n????

Notice the content is not gzipped and there are extra junk characters added on.

"?n????"

So far I can't figure out how to get it to return gzip. I think there is a mistake in the example code but I'm having trouble figuring it out.

Thank you in advance.

答案1

得分: 3

有人遇到了gzip的问题。他们的拉取请求解决了我的问题。这是gin框架gzip包中的一个错误。

在contrib/gzip/gzip.go文件中缺少了这个方法:

func (g *gzipWriter) WriteString(s string) (n int, err error) {
    return g.writer.Write([]byte(s))
}

原始问题链接,包括拉取请求:
https://github.com/gin-gonic/contrib/pull/59

一旦项目所有者接受了这个拉取请求,这个问题就会被解决。

英文:

Someone else had an issue with gzip. Their pull request resolved this problem for me. It's a bug in the gzip package for gin framework.

This method was missing from contrib/gzip/gzip.go

func (g *gzipWriter) WriteString(s string) (n int, err error) {
	return g.writer.Write([]byte(s))
}

Link to original issue that includes pull request.
https://github.com/gin-gonic/contrib/pull/59

Once the owner of the project accepts the pull request this issue will be resolved.

huangapple
  • 本文由 发表于 2016年7月19日 22:17:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/38461106.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定