I am sure this problem will have surfaced already, but I don’t know what exact terms to search for and my searches didn’t yield any results. I am trying to load a JSON file from Github. Those files are delivered as plain/text if I am right and I am receiving the error:
ERROR error calling resources.GetRemote: failed to resolve media type for remote resource "https://raw.githubusercontent.com/ai-robots-txt/ai.robots.txt/refs/heads/main/robots.json"
The layout (just trying to get it and debug it for now:
{{ $data := dict }}
{{ $url := "https://raw.githubusercontent.com/ai-robots-txt/ai.robots.txt/refs/heads/main/robots.json" }}
{{ with resources.GetRemote $url }}
{{ with .Err }}
{{ errorf "%s" . }}
{{ else }}
{{/* {{ $data = . | transform.Unmarshal }} */}}
{{ warnf "%#v" $data }}
{{ end }}
{{ else }}
{{ errorf "Unable to get remote resource %q" $url }}
{{ end }}
I can see the section in the docs talking about content type issues but I fail to understand or apply it here. I am using the same code, but as I wrote above, I am pretty sure it’s plaintext, not octet-stream or similar. The error occurs with and without that commented transform.Unmarshal line.
What am I overlooking? Basically, how can I retrieve a remote plaintext and then transform it into JSON?
I am on Ubuntu, and it returns error calling resources.GetRemote: failed to resolve media type for remote resource "https://api.github.com/repos/ai-robots-txt/ai.robots.txt/contents/robots.json" if I don’t check for an error:
{{ $url := "https://raw.githubusercontent.com/ai-robots-txt/ai.robots.txt/refs/heads/main/robots.json" }}
{{ with resources.GetRemote $url }}
{{ . }}
{{ end }}
The API way with $url being https://api.github.com/repos/ai-robots-txt/ai.robots.txt/contents/robots.json also results in the error.
I am getting more and more convinced that I have a weird setup issue going on. Loading all those URLs in a browser results in JSON and proper content types.
That seems to be the reason. I am getting results when I am running the layout on an empty site without other modules. I’ll investigate and write up what I did wrong or what interfered.
Apparently Hugo did not like that I took over the .json suffix and handled all JSON from here with application/feed+json? After commenting those lines out resources.GetRemote magically started working. I have now removed the new mediatype and moved to mediaType = "application/json" for the outputformat, using the internal one.
That works for now, but won’t fix the issue at hand: The feed needs to be delivered as application/feed+json AND with .json as extension and I have a feeling that once .json is taken it won’t be available for any other mediatype?
Hugo does not much in relation to delivering the file from the browser to the user so a quick solution is probably to just output it as whatever and then regulate the media type via server configuration.
I stumbled over more issues in my understanding of custom mediatypes. I guess I’ll re-read large parts of the docs.