Gin: How to return plain text with content type "application/json; charset=utf-8"?

Created on 22 Oct 2015  路  5Comments  路  Source: gin-gonic/gin

I know context.JSON() returns the serialised object with content type "application/json; charset=utf-8". But I want to skip serialising by returning cached plain text, because my object does not change constantly.

Is it possible to use something like context.String() but set content type "application/json; charset=utf-8"?

Thanks.

Most helpful comment

Odd. What about context.Data()?

context.Data(200, "application/json; charset=utf-8", []byte(someString))

All 5 comments

Check out context.Header(). You should be able to do this:

context.Header("Content-Type", "application/json; charset=utf-8")
context.String(...)

@mattprice Thanks for the reply. But it does not work because context.String() overwrites Content-Type.

Odd. What about context.Data()?

context.Data(200, "application/json; charset=utf-8", []byte(someString))

It looks promising. I will try. Thanks.

@mattprice It works. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

boneq picture boneq  路  3Comments

sofish picture sofish  路  3Comments

ghost picture ghost  路  3Comments

olegsobchuk picture olegsobchuk  路  3Comments

rawoke083 picture rawoke083  路  3Comments