0

I want to create a query parameter without escaping the query string.

For example, I need to create a query parameter q with the content

"before:{2019-12-24 19:57:34}"

so that the URL is

https://android-review.googlesource.com/changes/?q=before:{2019-12-24 19:57:34}

If I use this code (Golang Playground)

url, _ := url.Parse("https://android-review.googlesource.com/changes/")
q := url.Query()
q.Set("q", "before:{2019-12-24 19:57:34}")
url.RawQuery = q.Encode()
fmt.Println(url) 

url is escaping the special characters, spaces, and brackets:

https://android-review.googlesource.com/changes/?q=before%3A%7B2019-12-24+19%3A57%3A34%7D

How can I solve this issue except manually creating the URL (without query parameters then)?

Michael Dorner
  • 17,587
  • 13
  • 87
  • 117

1 Answers1

3

If you don't want your URL query to be encoded, then don't use the Encode() method. Instead, set the RawQuery value directly, yourself:

url, _ := url.Parse("https://android-review.googlesource.com/changes/")
url.RawQuery = "q=before:{2019-12-24 19:57:34}"
fmt.Println(url)

Output:

https://android-review.googlesource.com/changes/?q=before:{2019-12-24 19:57:34}

playground

Keep in mind, however, that this is a recipe for potential disaster, depending on how that url is eventually used. In particular, the space in that URL should be escaped, according to RFC. See more here.

Perhaps you'll want to implement your own minimal escaping, if that's compatible with your use-case.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189