Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gzip doesn't work #6

Open
mostafasolati opened this issue Mar 2, 2017 · 6 comments
Open

gzip doesn't work #6

mostafasolati opened this issue Mar 2, 2017 · 6 comments

Comments

@mostafasolati
Copy link

After enabling this middleware browser downloads page! and I can't see result in browser

@MTRNord
Copy link

MTRNord commented Jul 25, 2017

Have the same issue :/ Would love to use this

@easonlin404
Copy link

@mostafasolati @MTRNord it's work for me. Could you post your example code?

@MTRNord
Copy link

MTRNord commented Aug 2, 2017

@easonlin404

Hi my code is basicly (non functioning but you get the point what it does I think. Otherwise feel free to ask):

router := gin.Default()
router.Use(gzip.Gzip(gzip.DefaultCompression))
router.GET("/login", func (c *gin.Context) {
	templates.WritePageTemplate(c.Writer, &templates.LoginPage{
		Link: "something",
	})
})

A bit of explanation for the above: The templates.WritePageTemplate() function comes from https://github.com/valyala/quicktemplate and generates HTML via go files.

I expect to get a HTML file as I do without gzip but what happens is the following response and getting a download of a file named "document.gzip"

Response Headers (via chrome Network Tab):

Content-Encoding:gzip
Content-Length:239
Content-Type:application/x-gzip
Date:Wed, 02 Aug 2017 18:59:55 GMT
Vary:Accept-Encoding

@easonlin404
Copy link

easonlin404 commented Aug 6, 2017

@MTRNord @mostafasolati because net/http will detect contentType header if you don't to set. And you use quicktemplate which is not to set contentType, so you have to specify response header like:

package main

import (
	"fmt"
	"net/http"
	"time"

	"github.com/gin-contrib/gzip"
	"github.com/gin-gonic/gin"
	"github.com/valyala/quicktemplate/examples/basicserver/templates"
)

func main() {

	r := gin.Default()
	r.Use(gzip.Gzip(gzip.DefaultCompression))

	r.GET("/quick-template", func(c *gin.Context) {
		c.Writer.Header().Set("Content-Type", "text/html; charset=utf-8") // You have to set 'Content-Type'

		templates.WritePageTemplate(c.Writer, &templates.BasePage{})
	})

	// or you can use c.HTML, it will set Content-Type
	r.LoadHTMLFiles("templates/template.html")

	r.GET("/gin", func(c *gin.Context) {
		c.HTML(http.StatusOK, "template.html", gin.H{"values": fmt.Sprint(time.Now().Unix())})
	})

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

Test:

$ curl -v -H "Accept-Encoding: gzip" http://localhost:8080/quick-template

Will Output like:

*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET /quick-template HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.51.0
> Accept: */*
> Accept-Encoding: gzip
> 
< HTTP/1.1 200 OK
< Content-Encoding: gzip
< Content-Type: text/html; charset=utf-8
< Vary: Accept-Encoding
< Date: Sun, 06 Aug 2017 08:23:02 GMT
< Content-Length: 133
< 

@MTRNord
Copy link

MTRNord commented Aug 6, 2017

Ah ok thanks :) I will do so :D

@Programmerino
Copy link

Can we close this now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants