Skip to content

Header change not honored after middleware set it #83

@cajund

Description

@cajund

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions