Param inside getjson url

Hello, How can i be able to put something from a param into a json url ?

For example:

$query := {{ .Params.query}}

{{ $json := getJSON “https://www.example.com/api/$query

seems like i can’t put query inside the json “url” :frowning:

Hey, you could do something like this

{{ $url := printf "https://www.example.com/api/%s" .Params.query }}
{{ $json := getJSON $url }}

Hello, thank you for reply, it get the param but gives an error

execute of template failed: template: _default/single.html:4:12: executing "main" at <getJSON $url>: error calling getJSON: Failed to create request for getJSON resource https://www.udemy.com/api-2.0/courses/%!s(int=417914): parse https://www.udemy.com/api-2.0/courses/%!s(int=417914): invalid URL escape "%!s"

Ah. So since your param is an int, change

"https://www.example.com/api/%s"

to this

"https://www.example.com/api/%d"
2 Likes