gocraft / web

Go Router + Middleware. Your Contexts.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Header change not honored after middleware set it

cajund opened this issue · comments

Hi Folks,

I've wrapped your package in a library for a set of web services that always return JSON data (except for one case, this one, when downloading a file). I set the Content-Type with a piece of middleware:

rRouter := &Router{}
rRouter.mDispatch = web.New(Context{})
rRouter.mDispatch.Middleware(func(rw web.ResponseWriter, r *web.Request, next web.NextMiddlewareFunc) {
	rw.Header().Set("Content-Type", "application/json; charset=utf-8")
	next(rw, r)
})

In one of my handlers, I want to override this for a file download:

rw.WriteHeader(201)
bytes := getContent(filename)
rw.Header().Set("Content-Type","application/pdf")
rw.Header().Set("Content-Disposition", "Attachment; filename="filename")
rw.Write(bytes)

When the endpoint is called, the content type comes down as application/json and not application/pdf as expected. I also have verified that the content type is set properly in the rw.Header map:

log.Printf("%+v", rw.Header())
    2018/06/13 21:08:03 map[Content-Type:[application/pdf]]

If I remove the middleware, this works fine. Any suggestions or insight as to what may be happening? Am I working with a copy of the rw var?

Thanks in advance.