From b4fc35c3e7c67624905bc951881ee7575c3fef29 Mon Sep 17 00:00:00 2001 From: Joe Mooring Date: Mon, 6 May 2024 07:42:02 -0700 Subject: [PATCH] Change transform.Unmarshal remote data examples (#2557) --- content/en/functions/data/GetCSV.md | 6 +++--- content/en/functions/data/GetJSON.md | 6 +++--- content/en/functions/encoding/Base64Decode.md | 2 +- content/en/functions/resources/GetRemote.md | 12 ++++++++++- content/en/functions/transform/Unmarshal.md | 21 +++++++++++++------ 5 files changed, 33 insertions(+), 14 deletions(-) diff --git a/content/en/functions/data/GetCSV.md b/content/en/functions/data/GetCSV.md index 141ec6c72..c35d48f67 100644 --- a/content/en/functions/data/GetCSV.md +++ b/content/en/functions/data/GetCSV.md @@ -97,7 +97,7 @@ my-project/ {{ $p := "data/pets.csv" }} {{ with resources.Get $p }} {{ $opts := dict "delimiter" "," }} - {{ $data = .Content | transform.Unmarshal $opts }} + {{ $data = . | transform.Unmarshal $opts }} {{ else }} {{ errorf "Unable to get resource %q" $p }} {{ end }} @@ -121,7 +121,7 @@ my-project/ {{ $p := "pets.csv" }} {{ with .Resources.Get $p }} {{ $opts := dict "delimiter" "," }} - {{ $data = .Content | transform.Unmarshal $opts }} + {{ $data = . | transform.Unmarshal $opts }} {{ else }} {{ errorf "Unable to get resource %q" $p }} {{ end }} @@ -139,7 +139,7 @@ Consider using the [`resources.GetRemote`] function with [`transform.Unmarshal`] {{ errorf "%s" . }} {{ else }} {{ $opts := dict "delimiter" "," }} - {{ $data = .Content | transform.Unmarshal $opts }} + {{ $data = . | transform.Unmarshal $opts }} {{ end }} {{ else }} {{ errorf "Unable to get remote resource %q" $u }} diff --git a/content/en/functions/data/GetJSON.md b/content/en/functions/data/GetJSON.md index 1a661c38b..348021226 100644 --- a/content/en/functions/data/GetJSON.md +++ b/content/en/functions/data/GetJSON.md @@ -101,7 +101,7 @@ my-project/ {{ $data := dict }} {{ $p := "data/books.json" }} {{ with resources.Get $p }} - {{ $data = .Content | transform.Unmarshal }} + {{ $data = . | transform.Unmarshal }} {{ else }} {{ errorf "Unable to get resource %q" $p }} {{ end }} @@ -124,7 +124,7 @@ my-project/ {{ $data := dict }} {{ $p := "books.json" }} {{ with .Resources.Get $p }} - {{ $data = .Content | transform.Unmarshal }} + {{ $data = . | transform.Unmarshal }} {{ else }} {{ errorf "Unable to get resource %q" $p }} {{ end }} @@ -141,7 +141,7 @@ Consider using the [`resources.GetRemote`] function with [`transform.Unmarshal`] {{ with .Err }} {{ errorf "%s" . }} {{ else }} - {{ $data = .Content | transform.Unmarshal }} + {{ $data = . | transform.Unmarshal }} {{ end }} {{ else }} {{ errorf "Unable to get remote resource %q" $u }} diff --git a/content/en/functions/encoding/Base64Decode.md b/content/en/functions/encoding/Base64Decode.md index 99b231513..821ca805a 100644 --- a/content/en/functions/encoding/Base64Decode.md +++ b/content/en/functions/encoding/Base64Decode.md @@ -30,7 +30,7 @@ To retrieve and render the content: {{ with .Err }} {{ errorf "%s" . }} {{ else }} - {{ with .Content | transform.Unmarshal }} + {{ with . | transform.Unmarshal }} {{ .content | base64Decode | markdownify }} {{ end }} {{ end }} diff --git a/content/en/functions/resources/GetRemote.md b/content/en/functions/resources/GetRemote.md index d140797ac..791394ed3 100644 --- a/content/en/functions/resources/GetRemote.md +++ b/content/en/functions/resources/GetRemote.md @@ -79,13 +79,23 @@ When retrieving remote data, use the [`transform.Unmarshal`] function to [unmars {{ with .Err }} {{ errorf "%s" . }} {{ else }} - {{ $data = .Content | transform.Unmarshal }} + {{ $data = . | transform.Unmarshal }} {{ end }} {{ else }} {{ errorf "Unable to get remote resource %q" $url }} {{ end }} ``` +{{% note %}} +When retrieving remote data, a misconfigured server may send a response header with an incorrect [Content-Type]. For example, the server may set the Content-Type header to `application/octet-stream` instead of `application/json`. + +In these cases, pass the resource `Content` through the `transform.Unmarshal` function instead of passing the resource itself. For example, in the above, do this instead: + +`{{ $data = .Content | transform.Unmarshal }}` + +[Content-Type]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type +{{% /note %}} + ## Error handling The [`Err`] method on a resource returned by the `resources.GetRemote` function returns an error message if the HTTP request fails, else nil. If you do not handle the error yourself, Hugo will fail the build. diff --git a/content/en/functions/transform/Unmarshal.md b/content/en/functions/transform/Unmarshal.md index 5786a1dcb..998152eb2 100644 --- a/content/en/functions/transform/Unmarshal.md +++ b/content/en/functions/transform/Unmarshal.md @@ -49,7 +49,7 @@ assets/ {{ $data := dict }} {{ $path := "data/books.json" }} {{ with resources.Get $path }} - {{ with .Content | transform.Unmarshal }} + {{ with . | transform.Unmarshal }} {{ $data = . }} {{ end }} {{ else }} @@ -78,7 +78,7 @@ content/ {{ $data := dict }} {{ $path := "books.json" }} {{ with .Resources.Get $path }} - {{ with .Content | transform.Unmarshal }} + {{ with . | transform.Unmarshal }} {{ $data = . }} {{ end }} {{ else }} @@ -101,7 +101,7 @@ A remote resource is a file on a remote server, accessible via HTTP or HTTPS. {{ with .Err }} {{ errorf "%s" . }} {{ else }} - {{ $data = .Content | transform.Unmarshal }} + {{ $data = . | transform.Unmarshal }} {{ end }} {{ else }} {{ errorf "Unable to get remote resource %q" $url }} @@ -112,8 +112,15 @@ A remote resource is a file on a remote server, accessible via HTTP or HTTPS. {{ end }} ``` -[resource]: /getting-started/glossary/#resource -[page bundle]: /content-management/page-bundles/ +{{% note %}} +When retrieving remote data, a misconfigured server may send a response header with an incorrect [Content-Type]. For example, the server may set the Content-Type header to `application/octet-stream` instead of `application/json`. + +In these cases, pass the resource `Content` through the `transform.Unmarshal` function instead of passing the resource itself. For example, in the above, do this instead: + +`{{ $data = .Content | transform.Unmarshal }}` + +[Content-Type]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type +{{% /note %}} ## Options @@ -172,7 +179,7 @@ Get the remote data: {{ with .Err }} {{ errorf "%s" . }} {{ else }} - {{ $data = .Content | transform.Unmarshal }} + {{ $data = . | transform.Unmarshal }} {{ end }} {{ else }} {{ errorf "Unable to get remote resource %q" $url }} @@ -290,3 +297,5 @@ Hugo renders this to: [`index`]: /functions/collections/indexfunction/ [identifiers]: https://go.dev/ref/spec#Identifiers +[resource]: /getting-started/glossary/#resource +[page bundle]: /content-management/page-bundles/