Fetching remote json

Since updating from v.138 to v0.147.3+extended+withdeploy, one of my pages, which uses remote data, won’t build properly.

The setup which had been working:

  • a page at content/bdr.md had a frontmatter layout which specified api
  • there was a layouts/_default/api.html file, which contained code fetching remote json:
{{ $url := "https://repository.library.brown.edu/api/collections/bdr:88np2gke" }}
{{ with try (resources.GetRemote $url) }}
  {{ with .Err }}
    {{ errorf "%s" . }}
  {{ else with .Value }}
    {{ $data = . | transform.Unmarshal }}
    <ul>
{{ range $artifacts := $data.items.docs }}
  <li>
    <a href='{{ $artifacts.uri }}'>
      {{ $artifacts.primary_title }}
    </a>
    </li>
{{ end }}
</ul>

  {{ end }}

{{ else }}
  {{ errorf "Unable to get remote resource %q " $url }}
{{ end }}

Seeing the breaking changes in 146, I moved my api.html file to layouts, and was still getting the error execute of template failed: template: api.html:15:28: executing "api.html" at <$data.items.docs>: can't evaluate field docs in type interface {} when I tried to build the site.

I tried to make the api.html file into a content/bdr/api.gotmpl file, and while that got rid of the build error and built the page itself, the loop didn’t produce any content, and I don’t think that’s the right approach, since I’m not trying to create multiple Hugo pages, just render what’s in the remote json. I’ve tried putting <pre>{{ debug.Dump $data }}</pre> in right after the transform.Unmarshal function, but I don’t get any debugging info. There’s no error or warning in the console or terminal. I’ve searched the forum and read all the topics on the recent template restructuring.

So, having layouts/_default/api.html or layouts/api.html should behave just the same, but you did the right thing moving it to the top level.

I don’t understand how the template related changes has anything to do with your problem. It finds the correct layout.

Are you sure your $data variable contains what you expect?

the note at the end of this section may be relevant.

I tested with your code and it works great. Try it:

git clone --single-branch -b hugo-forum-topic-54736 https://github.com/jmooring/hugo-testing hugo-forum-topic-54736
cd hugo-forum-topic-54736
hugo server

Okay, I feel insane. Our code is identical, it is clearly something I haven’t thought of. I’ve made the repo public if you want to take a look at branch hugo-146.

I didn’t think the template changes were what caused the problem, but it was the only thing I changed after reading the release notes for 146, and I didn’t see any other breaking changes that were relevant. I haven’t been able to get debug.Dump to produce output for $data and I’ve reviewed the json that I’m fetching, so I’m pretty sure that the structure s right.

Here’s what I did:

git clone --recurse-submodules https://github.com/Brown-University-Library/u2
cd u2/
git checkout hugo-146
hugo server

Here’s what I got:

I’m wondering if your cache is corrupt. On my system, the remote resource that we captured is here:

/home/jmooring/.cache/hugo_cache/u2/filecache/getresource/11509215134352248767

You could try clearing the getresource cache and building again:

rm  ~/.cache/hugo_cache/u2/filecache/getresource/*

Learn more about file caches here:
https://gohugo.io/configuration/caches/

1 Like

That seems to have done the trick! Thanks, I never would have figured that out on my own. (I did have to do some hunting in the docs to find out where the cache directory is for me, but that was straightforward enough.)

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