Setting a custom response header in a Gin response

Written by Ben Wendt

I’m using Gin Gonic on a project I’m working on at work these days. I was in a bit of a pickle where I had to specify a response body that I already had as a string, but I needed to put the correct content type header on the response (meaning I couldn’t use the built in .String or .JSON methods.

I had to dig around a bit in the source code a bit to figure this one out. So, If you want to set a custom response header on a gin response, do it like this

imports (
func whatever() {
        http.StatusOK, render.Data{
            ContentType: "application/json",
            Data:        []byte(response),