I'm interacting with an API which takes URI components that may contain both forward-slashes and spaces. I need to percent-encode this URI component so that both the forward-slashes and spaces are properly encoded. Example Go code can be found here.
I turned to net/url
to tackle this, but turns out it doesn't do what I want, apparently.
url.QueryEscape()
is intended for query strings, and as such it turns whitespace into+
rather than%20
.url.Parse()
andurl.ParseRequestUri()
will turn whitespace into%20
, but it will ignore/
, as it should.
Since I'm very likely to muck this up (escaping is non-trivial), I'd prefer I relied upon some standard library out there to do this for me. Does such a standard method exist in a well-tested Go library?
Side note: The behaviour I'm seeking is identical to JS's encodeURIcomponent
.