getJSON authentication headers with multiple keys?

I see the docs on how to add headers, however I need multiple keys, with one value each.

For example, I need to send

req.Header.Add("X-RapidAPI-Key", "MYKEY")
req.Header.Add("X-RapidAPI-Host", "api.host.com")

And it seems the solution in the docs only shows multiple values per one key. What is the recommended method (if one exists) to send both? Something like this but probably not this?

{{ $data := getJSON "https://example.org/api" (dict "Authorization" "X-RapidAPI-Key MYKEY X-RapidAPI-Host api.host.com") }}

With getJSON I think you can do:

{{ $url := "https://example.org/books.json" }}
{{ $opts := dict
  "X-RapidAPI-Key" "MYKEY"
  "X-RapidAPI-Host" "api.host.com"
}}
{{ $data := getJSON $url $opts }}

But both getJSON and getCSV have been superseded by resources.GetRemote:

{{ $url := "https://example.org/books.json" }}
{{ $opts := dict
  "headers" (dict "X-RapidAPI-Key" "MYKEY" "X-RapidAPI-Host" "api.host.com")
}}
{{ $resource := resources.GetRemote $url $opts }}

One of the advantages of resources.GetRemote is error checking, where you can choose to either ignore errors (with warnf) or fail the build (with errorf or erroridf).

{{ $data := "" }}
{{ $u := "https://example.org/books.json" }}
{{ with resources.GetRemote $u }}
  {{ with .Err }}
    {{ errorf "%s" . }}
  {{ else }}
    {{ $data = . | transform.Unmarshal }}
  {{ end }}
{{ else }}
  {{ errorf "Unable to get remote resource %q" $u }}
{{ end }}
3 Likes

I’ll use the .GetRemote version, thank you!

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