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

Not escaping JSON when rendering responses #894

Closed
tspecht opened this issue Apr 21, 2017 · 8 comments
Closed

Not escaping JSON when rendering responses #894

tspecht opened this issue Apr 21, 2017 · 8 comments

Comments

@tspecht
Copy link

tspecht commented Apr 21, 2017

Hey guys,

I'm currently trying to render out JSON as a response to a request which contains an & in one of the fields which is a pagination URL. Unfortunately when rendering this out to the clients it seems like this character (and others, this is just an example) are getting escaped by the JSON writer used. I tracked it down to this function in json.go:

func WriteJSON(w http.ResponseWriter, obj interface{}) error {
	writeContentType(w, jsonContentType)
	return json.NewEncoder(w).Encode(obj)
}

When I change the function to

func WriteJSON(w http.ResponseWriter, obj interface{}) error {
	writeContentType(w, jsonContentType)
	encoder := json.NewEncoder(w)
	encoder.SetEscapeHTML(false)
	return encoder.Encode(obj)
}

everything seems to work as expected.

Before I submit a pull-request to set a flag somewhere to configure this behaviour, I wanted to quickly check in if this is wanted behaviour?

@thinkerou
Copy link
Member

Hi @tspecht have you example code?

@javierprovecho
Copy link
Member

this will be solved through #694 which is actually on hold, need to stabilize other issues/pr first

@geoherna
Copy link

geoherna commented Oct 5, 2017

Any updates on this?

@thinkerou
Copy link
Member

@tspecht @geoherna #694 have merged to master branch, please retry it, closing. thanks!

@bjm88
Copy link

bjm88 commented Dec 17, 2018

I can't find PureJSON function on context...did this get renamed or ever in release?

@thinkerou
Copy link
Member

@bjm88
Copy link

bjm88 commented Dec 17, 2018

Is this in master only or a release build....

@nanmu42
Copy link

nanmu42 commented Apr 18, 2019

As of now(v1.3.0), This feature is still only existing in master, not in a release. 😞

Related: golang/go#14749

Workaround:

var (
		buf     bytes.Buffer
		encoder = json.NewEncoder(&buf)
	)

        // important
	encoder.SetEscapeHTML(false)

	_ = encoder.Encode(responseData)

	c.DataFromReader(http.StatusOK, int64(buf.Len()), "application/json; charset=utf-8", &buf, map[string]string{})

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

6 participants