I have an API from which I’m trying to get data, but first, a request must be made that assigns a cookie to the client, which needs to be passed in subsequent requests, i.e.:
https://site.org/ → cookie assigned
After that, I can make requests to:
https://site.org/api
But only if the cookie from the first request is passed. Is this possible with Hugo?
–
Right now, instead of using an adapter that runs during the build and forms a map with data from the API, I use a custom Go file that runs (go run init.go) in CI before the direct call to hugo build and saves the necessary JSON files for the site. But I would prefer to do this directly using Hugo.
I don’t like posting on forums because as soon as I ask a question, the answer is immediately found.
{{ $initUrl := "https://site.org" }}
{{ $initOpt := dict "responseHeaders" (slice "set-cookie") }}
{{ $apiUrl := "https://site.org/api" }}
{{ with (resources.GetRemote $initUrl $initOpt )}}
{{ with (resources.GetRemote $apiUrl (dict "headers" (dict "Cookie" (index .Data.Headers "Set-Cookie" )))) }}
{{ . | unmarshal }}
{{ end }}
{{ end }}
Basically, the solution boils down to a snippet like this, which solves my problem. I was using version 0.139 of Hugo, but I found the commit with the necessary functionality, which is available from version 0.143 onwards.