Hugo fails to fetch JSON resource (from a local server)

I’m building a website using strapi, with the help of this (outdated but still helpful) tutorial.

When I run hugo server, I get this error:

ERROR 2023/08/20 02:15:10 Failed to get JSON resource "http://127.0.0.1:1337/api/about?locale=en": Get "http://127.0.0.1:1337/api/about?locale=en": dial tcp 127.0.0.1:1337: connect: connection refused
If you feel that this should not be logged as an ERROR, you can ignore it by adding this to your site config:
ignoreErrors = ["error-remote-getjson"]

(It repeats six times, which is the number of pages I have).

Strangely, when I run it a second time (without restarting strapi), I get a different error, this time only twice, then an infinite recursion error after a delay:

Start building sites … 
hugo v0.111.3+extended linux/amd64 BuildDate=2023-03-16T08:41:31Z VendorInfo=debian:0.111.3-1
ERROR 2023/08/20 02:15:18 Failed to get JSON resource "http://127.0.0.1:1337/api/about?locale=en": Get "http://127.0.0.1:1337/api/about?locale=en": read tcp 127.0.0.1:39144->127.0.0.1:1337: read: connection reset by peer
If you feel that this should not be logged as an ERROR, you can ignore it by adding this to your site config:
ignoreErrors = ["error-remote-getjson"]
ERROR 2023/08/20 02:15:18 Failed to get JSON resource "http://127.0.0.1:1337/api/home?locale=en": Get "http://127.0.0.1:1337/api/home?locale=en": read tcp 127.0.0.1:39170->127.0.0.1:1337: read: connection reset by peer
If you feel that this should not be logged as an ERROR, you can ignore it by adding this to your site config:
ignoreErrors = ["error-remote-getjson"]
Error: Error building site: failed to render pages: render of "page" failed: "/home/cyrille/Dev/Perso/Web/strapi-hugo-blog/docs-app/themes/book/layouts/_default/baseof.html:24:9": execute of template failed: template: _default/single.html:24:9: executing "_default/single.html" at <partial "docs/inject/content-after" .>: error calling partial: partial "docs/inject/content-after" timed out after 30s. This is most likely due to infinite recursion. If this is just a slow template, you can try to increase the 'timeout' config setting.
Built in 30065 ms

In config.toml, I have tried to set my strapi server URL to http://127.0.0.1:1337/api and http://localhost:1337/api, I get the same error.

When I open for example http://127.0.0.1:1337/api/about?locale=en in a browser, my JSON data appears and I can see the request in the console where Strapi is running. I see nothing when trying to start the Hugo server.

Here is the code of the partial that fetches the data:

<!-- Partial to fetch content from Strapi. -->

{{ $endpoint := $.Param "endpoint" }}
{{ $data := dict "title" "" "content" "" }}

{{ if and $endpoint .Site.Params.StrapiServerURL }}

{{ $contentURL := printf "%s%s" .Site.Params.StrapiServerURL $endpoint }}
{{ $data = getJSON $contentURL }}

{{ end }}

{{ return $data }}

I’m not sure what’s the problem in your case, but in general I would recommend resources.GetRemote (a newer and more robust template function) for this and similar stuff:

{{ $data = resources.GetRemote $url | transform.Unmarshal }}

For error handling, see

Code updated:

<!-- Partial to fetch content from Strapi. -->

{{ if and $endpoint .Site.Params.StrapiServerURL }}

  {{ $contentURL := printf "%s%s" .Site.Params.StrapiServerURL $endpoint }}
  {{/* $data = getJSON $contentURL */}}
  {{ $remoteResource := resources.GetRemote $contentURL }}
  {{ with $remoteResource.Err }}
    {{ warnf "%s" . }}
  {{ else }}
    {{ $data = transform.Unmarshal . }}
  {{ end }}

{{ end }}

{{ return $data }}

Now, the server is running because the errors turned into warnings:

WARN 2023/08/25 17:34:29 error calling resources.GetRemote: Get "http://localhost:1337/api/home?locale=en": dial tcp [::1]:1337: connect: connection refused
WARN 2023/08/25 17:34:29 error calling resources.GetRemote: Get "http://localhost:1337/api/about?locale=en": dial tcp [::1]:1337: connect: connection refused
WARN 2023/08/25 17:34:29 error calling resources.GetRemote: Get "http://localhost:1337/api/terms?locale=en": dial tcp [::1]:1337: connect: connection refused
WARN 2023/08/25 17:34:29 error calling resources.GetRemote: Get "http://localhost:1337/api/home?locale=fr-FR": dial tcp [::1]:1337: connect: connection refused
WARN 2023/08/25 17:34:29 error calling resources.GetRemote: Get "http://localhost:1337/api/about?locale=fr-FR": dial tcp [::1]:1337: connect: connection refused
WARN 2023/08/25 17:34:29 error calling resources.GetRemote: Get "http://localhost:1337/api/terms?locale=fr-FR": dial tcp [::1]:1337: connect: connection refused

But how can I have more details about these errors?

I’ve got the exact same error messages when my Strapi server is turned off. So it seems the problem comes from Hugo or its runtime.

Not sure how you reached that conclusion…

I can successfully connect to a fresh installation of Strapi.

$ node -v
v20.5.1
$ npm -v
9.8.1

When I stop the Strapi server, Hugo throws an error, as expected. Let’s compare your error to mine:

YOU: dial tcp [::1]:1337: connect: connection refused
ME:  dial tcp 127.0.0.1:1337: connect: connection refused

Your request is resolving to an IPV6 address.

In fact, when in config.toml, I set StrapiServerURL = 'http://127.0.0.1:1337/api', I have your error too:

dial tcp 127.0.0.1:1337: connect: connection refused

[::1] is when I write StrapiServerURL = 'http://localhost:1337/api'.

Well, Strapi API works correctly: I can access it through my browser and Postman. I can’t see any request made by Hugo when it is running. When I shut it down, the error message is exactly the same, as if it changed nothing. That’s why I think Hugo can’t reach it for some reason on its side.

So, it’s likely a configuration or environment issue. You might poke around the Strapi forum and/or Discord channel.

I already posted on Strapi Discord Server and someone told me it was probably Hugo’s fault ^^’

I just made some tests with beeceptor.com and noticed that on Beeceptor side, I only received requests the first time I launched hugo server. On Hugo side, I get the same error messages every time as if they were cached. This sounded like the most plausible explanation of what was going on.

I checked my config.toml file and found out this:

[caches]
[caches.getjson]
# Sets the maximum age of cache to 10s before it is cleared.
maxAge = "10s"

So I added this:

[caches.getresource]

and now I can see my new requests on the mock API side!

However, when I go back to using Strapi as main URL, again I can’t see my requests on Strapi’s side.

OK this was a really dumb issue. In fact, my hugo folder was inside my strapi folder. When I noticed it, I told myself “bah, this should work anyway”. In fact, no, because as a development server, strapi restarted each time a file was changed. I didn’t see it because after restarting, it looked the same in the console, as if nothing had happened.

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