Go rendering URL RowQuery string in a template - different behaviours
Asked Answered
P

1

6

I 'm new to go and I got my mind stuck with something pretty trivial, so I ask for your help, maybe anyone knows why this behaviour happens:

I want to output the query params string of an url object in a template, but apparently because of the "?" char placed in front of the variable, the (whole) output is url encoded. Does question mark has a special meaning in go templates?

instead of param1=value1&value2=param2, output is param1%3dvalue1%26value2%3dparam2

Example in go playground

Pharisaism answered 28/6, 2017 at 10:34 Comment(0)
B
7

The Go documentation of the html/template package explains the behaviour (https://golang.org/pkg/html/template/):

The security model used by this package assumes that template authors are trusted, while Execute's data parameter is not. More details are provided below.

The thing about this is that you should really think twice about why you don't want Go to apply this security behaviour to your template.

Then, if you're really sure you don't want to escape the string use template.URL intead of a string: https://play.golang.org/p/kfv2tH6WDG

Boardinghouse answered 28/6, 2017 at 10:42 Comment(3)
I found this golang.org/pkg/html/template/#hdr-Contexts, I think this is the reason, but how do I instruct go not to apply by default the "| urlquery" filter?Pharisaism
"The thing about this is that you should really think twice about why you don't want Go to apply this security behaviour to your template." Because in my case the RawQuery as URL struct documentation states, is already encoded so it's safe....Pharisaism
Then you may simply use text/template in your program instead of html/template, see play.golang.org/p/_f1chjA3M6Shouldst

© 2022 - 2024 — McMap. All rights reserved.