Encode part of Url query params

I need to encode only part of the query string (after the “link” parameter)

{{- $url = "/ds-and-mlops/?target=iframe_mlrun_docs&link="https://docs.mlrun.org/en/release-v0.6.x-latest/quick-start.html#working-with-mlrun" -}}

I need this code

{{- $url = "/ds-and-mlops/?target=iframe_mlrun_docs&link="https%3A%2F%2Fdocs.mlrun.org%2Fen%2Frelease-v0.6.x-latest%2Fquick-start.html%23working-with-mlrun" -}}

How can I make this?

Have you checked the source of the generated HTML?

Go’s html/template package does automatic encoding based on the context of the code.

Also have a look at the htmlEscape function and this topic.

Yes, I checked, it doesn’t encode. And I only need to encode this part "link="https://docs.mlrun.org/en/release-v0.6.x-latest/quick-start.html#working-with-mlrun"
not all the url.

You need to split the URL, do the encoding and then append it to the rest of the URL.

There is no other way I am afraid since you need to enocde only part of the URL.

Also please wrap your code either in backticks: ` or use the </> button in the post UI, otherwise the forum software does not show raw HTML.

Thanks for your answer. It’s not a problem to split the URL. But I don’t know how to encode the part of the URL I need.

I already told you how to do the encoding above. You would need to use a combination of the htmlEscape and the replace functions. See the above links one more time:

{{ $url := `link="https://docs.mlrun.org/en/release-v0.6.x-latest/quick-start.html#working-with-mlrun"` }}
{{ $url = htmlEscape $url }}
{{ $url = replace $url ":" "%3A" }}
{{ $url = replace $url "/" "%2F" }}
1 Like

Thanks!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.