mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-21 21:35:28 +02:00
Merge commit '5be51ac3db225d5df501ed1fa1499c41d97dbf65'
This commit is contained in:
@@ -3,19 +3,11 @@ title: resources.GetRemote
|
||||
description: Returns a remote resource from the given URL, or nil if none found.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: []
|
||||
related:
|
||||
- functions/data/GetCSV
|
||||
- functions/data/GetJSON
|
||||
- functions/resources/ByType
|
||||
- functions/resources/Get
|
||||
- functions/resources/GetMatch
|
||||
- functions/resources/Match
|
||||
- methods/page/Resources
|
||||
returnType: resource.Resource
|
||||
signatures: ['resources.GetRemote URL [OPTIONS]']
|
||||
toc: true
|
||||
params:
|
||||
functions_and_methods:
|
||||
aliases: []
|
||||
returnType: resource.Resource
|
||||
signatures: ['resources.GetRemote URL [OPTIONS]']
|
||||
---
|
||||
|
||||
{{< new-in 0.141.0 >}}
|
||||
@@ -69,11 +61,10 @@ The `resources.GetRemote` function takes an optional map of options.
|
||||
|
||||
## Options examples
|
||||
|
||||
{{% note %}}
|
||||
For brevity, the examples below do not include [error handling].
|
||||
> [!note]
|
||||
> For brevity, the examples below do not include [error handling].
|
||||
|
||||
[error handling]: #error-handling
|
||||
{{% /note %}}
|
||||
|
||||
To include a header:
|
||||
|
||||
@@ -148,15 +139,12 @@ When retrieving remote data, use the [`transform.Unmarshal`] function to [unmars
|
||||
{{ 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 %}}
|
||||
> [!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 }}`
|
||||
|
||||
## Error handling
|
||||
|
||||
@@ -164,9 +152,8 @@ Use the [`try`] statement to capture HTTP request errors. If you do not handle t
|
||||
|
||||
[`try`]: /functions/go-template/try
|
||||
|
||||
{{% note %}}
|
||||
Hugo does not classify an HTTP response with status code 404 as an error. In this case `resources.GetRemote` returns nil.
|
||||
{{% /note %}}
|
||||
> [!note]
|
||||
> Hugo does not classify an HTTP response with status code 404 as an error. In this case `resources.GetRemote` returns nil.
|
||||
|
||||
```go-html-template
|
||||
{{ $url := "https://broken-example.org/images/a.jpg" }}
|
||||
@@ -206,9 +193,7 @@ The [`Data`] method on a resource returned by the `resources.GetRemote` function
|
||||
|
||||
Resources returned from `resources.GetRemote` are cached to disk. See [configure file caches] for details.
|
||||
|
||||
By default, Hugo derives the cache key from the arguments passed to the function, the URL and the options map, if any.
|
||||
|
||||
Override the cache key by setting a `key` in the options map. Use this approach to have more control over how often Hugo fetches a remote resource.
|
||||
By default, Hugo derives the cache key from the arguments passed to the function. Override the cache key by setting a `key` in the options map. Use this approach to have more control over how often Hugo fetches a remote resource.
|
||||
|
||||
```go-html-template
|
||||
{{ $url := "https://example.org/images/a.jpg" }}
|
||||
@@ -217,7 +202,7 @@ Override the cache key by setting a `key` in the options map. Use this approach
|
||||
{{ $resource := resources.GetRemote $url $opts }}
|
||||
```
|
||||
|
||||
[configure file caches]: /getting-started/configuration/#configure-file-caches
|
||||
[configure file caches]: /configuration/caches/
|
||||
|
||||
## Security
|
||||
|
||||
@@ -245,7 +230,7 @@ mediaTypes = ['^image/avif$','^application/vnd\.api\+json$']
|
||||
Note that the entry above is:
|
||||
|
||||
- An _addition_ to the allowlist; it does not _replace_ the allowlist
|
||||
- An array of regular expressions
|
||||
- An array of [regular expressions](g)
|
||||
|
||||
[allowlist]: https://en.wikipedia.org/wiki/Whitelist
|
||||
[Content-Type]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type
|
||||
|
Reference in New Issue
Block a user