Error for getJSON when used with Resources.getResources

Hello,

I’m getting error when used getJSON along with Resources.getResources on HUGO build.

Error:

ERROR Failed to get JSON resource “https://example.org/sitemap_9277466184542343073.json”: Failed to retrieve remote file: Not Found

Code:

{{$siteUrl := "https://api.publicapis.org/entries"}}
{{$siteData := getJSON (resources.GetRemote $siteUrl (dict 
  "method" "get"
)).Permalink}}

This error occurs only when we try to use hugo build command, for hugo server command it works fine.

I would recommend that you keep your topic about one thing. It may sound more effective to put totally unrelated questions in there at once, but it isn’t.

Hello @bep ,

I agree with you, I will create different topic for that.
I have updated the description.

Thank you.

You’re mixing two APIs here.

I would call getJSON a legacy API by know. We may eventually hide it from the docs.

I recommend you use resources.GetRemote for “everything”.

But to get a map from JSON, you need one extra step, e.g.

{{ with resources.GetRemote $siteUrl  }}
{{ $siteData := . | transform.Unmarshal }}
{{ end }}
6 Likes

Thank you @bep .

That solves the problem.

After seeing this, I changed my shortcodes for embedding static tweets from using getJSON to using resources.GetRemote. While that worked, I did notice one key difference. Here is an ultra-simplified code block showing the two:

{{/* With `getJSON` ... */}}
{{ $json := getJSON $urlToGet }}
{{ $text := .Page.RenderString $json.text }}
{{/* [etc.] */}}

{{/* With `resources.GetRemote` ... */}}
{{ with $resources.GetRemote $urlToGet }}
	{{ $json := unmarshal .Content }}
	{{ $text := $json.text | markdownify }}
	{{/* [etc.] */}}
{{ end }}

The difference was that .Page.RenderString caused errors when I tried to use it with resources.GetRemote, so I had to use markdownify instead. However, I seemed to get somewhat better results when I was using .Page.RenderString with getJSON — so is there some approach that would allow use of at least some form of .RenderString with .resources.GetRemote?

Wouldn’t this change the context so that .Page is not available anymore? If you store the page in $curPage and then do {{ $text := $json.text | $curPage.RenderString }} does it work?

2 Likes

Yes, sir, that works perfectly well. Thanks as always!

Couldn’t you also do $.Page.RenderString from within the with?

Nope, that was the problem. But @cshoredaniel had the answer.

Not .Page.RenderString which is what you had… i mean $.Page.RenderString which is top level context, even from within the with. Notice the leading $ which is the top level context variable anywhere. It’s not always the right approach, depending on partials, shortcodes, and what level in them you are at, but it sometimes can be the right approach.

2 Likes

I tried that, too. No joy.

Well, disregard — thought I had, but apparently not, because now I can get that to work, too. :man_shrugging:

1 Like

I tend to use the $curPage method because I am frequently in a partial from a shortcode and other contexts where $. doesn’t work as expected, and it is easier for me to be use something that works consistently in my use cases, but thank you for reminding that in simpler cases that it is available.

1 Like

Yes, exactly what I was trying to communicate with the “edge cases” where that $ won’t work as expected. And yea you have a good point about consistency and expectation.

2 Likes

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