GetRemote cannot pass Headers: Bad Request

With the new GetRemote I thought it would be possible to get a token of the Spotify API, but I don’t know how to get it to work :frowning:

This is my shortcode spotify.html so far:

{{/* spotify.html

  spotify album 1KAg47NePhjsKC4Y8ZC9z3
  spotify show 1O9vyJwNvUcdq1d9vFblQw 0
  spotify playlist 37i9dQZF1DXc51TI5dx7RC
  spotify track 2TpxZ7JUBn3uw46aR7qd6V
{{- $_time := "" -}}
{{- $market := "DE" -}}
{{- $type := index .Params 0 -}}
{{- $code := index .Params 1 -}}
{{- $time := index .Params 2 | default "" -}}
{{- $code_safejs := safeJS (md5 (printf "%s_%s" $type $code)) -}}

{{ $base64 := base64Encode (printf "%s:%s" (getenv "HUGO_SECRET_SPOTIFY_ID") (getenv "HUGO_SECRET_SPOTIFY_SECRET")) }}
{{ $base64_2 := printf "%s %s" "Basic" $base64 }}


{{ $postResponse := resources.GetRemote "" (dict
    "method" "post"
    "body" `{"form": "grant_type=client_credentials"}`
    "headers" (dict
        "Content-Type" "application/x-www-form-urlencoded"
        "Authorization" $base64_2

{{ $postResponse }}

This is the shortcode in use in a content/

{{< spotify album 1KAg47NePhjsKC4Y8ZC9z3 >}}

But instead of a proper Token I’m getting a Bad Request, and I can’t seem to figure out the right syntax. With curl it’s easy:

$ curl -X "POST" -H "Authorization: Basic xyz" -d 'grant_type=client_credentials'

How can I “translate” the -d Flag to the body (?) in the shortcode? I also had a look at the go http package, but I’m not getting it :frowning:

1 Like

I am also seeing the Bad Request error.

In my case I was able to work around it by using getJSON (since I wanted to retrieve a JSON anyway).

It appears that for some reason passing headers to GetRemote does not currently work.

I meant to file a bug report but at the moment my time is very short.

I amended the topic title, as this is not something about Spotify’s API.

Oh, ok :see_no_evil:

Then I’m very interested in how you got the token with getJSON. I tried this as well, but how do you use it for POST-Requests? I thought it’s only for GET-Requests?

Actually I used the example from the documentation:

Ok, thanks, but imho this is only for GET-Requests. So not useful to get a token first :slight_smile:

{{ $opts := dict
  "method" "post"
  "headers" (dict
    "Authorization" $base64_2
    "Content-Type" "application/x-www-form-urlencoded"
  "body" "grant_type=client_credentials"
{{ $postResponse := resources.GetRemote "" $opts }}

I have no idea why it’s working now if you move stuff around and use variables, but thank you very much! I’ve got it working now :tada:

Hint: if you want to use the result of $postResponse for the next getJSON use transform.Unmarshal:

{{ $postResponse := resources.GetRemote "" $opts | transform.Unmarshal }}
{{ $headers := dict "Authorization" (printf "Bearer %s" $postResponse.access_token) }}
{{ $json := getJSON "" $type "s/" $code "?market=" $market $headers }}

You set the body to this:

{"form": "grant_type=client_credentials"}

I set the body to this:


I think I tried this yesterday as well, but maybe I just needed to sleep :smiley: thanks again!

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