Debian Codegraph @

docs/content/en/functions/resources/GetRemote.md

raw
1 ---
2 title: resources.GetRemote
3 description: Returns a remote resource from the given URL, or nil if none found.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 aliases: []
9 returnType: resource.Resource
10 signatures: ['resources.GetRemote URL [OPTIONS]']
11 ---
12
13 {{< new-in 0.141.0 >}}
14 The `Err` method on the returned resource was removed in v0.141.0.
15
16 Use the [`try`](/functions/go-template/try/) statement instead, as shown in the [error handling](#error-handling) example below.
17 {{< /new-in >}}
18
19 ```go-html-template
20 {{ $url := "https://example.org/images/a.jpg" }}
21 {{ with try (resources.GetRemote $url) }}
22 {{ with .Err }}
23 {{ errorf "%s" . }}
24 {{ else with .Value }}
25 <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
26 {{ else }}
27 {{ errorf "Unable to get remote resource %q" $url }}
28 {{ end }}
29 {{ end }}
30 ```
31
32 When you publish a remote resource with the [`Permalink`][], [`RelPermalink`][], or [`Publish`][] methods as shown above, Hugo places the resulting file in the root of [`publishDir`][] with the basename of the URL. To ensure unique cache keys, Hugo appends a hash to the original filename.
33
34 ## Options
35
36 The `resources.GetRemote` function accepts an options map.
37
38 `body`
39 : (`string`) The data you want to transmit to the server.
40
41 `headers`
42 : (`map[string][]string`) The collection of key-value pairs that provide additional information about the request.
43
44 `key`
45 : (`string`) The cache key. Hugo derives the default value from the URL and options map. See [caching](#caching).
46
47 `method`
48 : (`string`) The action to perform on the requested resource, typically one of `GET`, `POST`, or `HEAD`.
49
50 `responseHeaders`
51 : {{< new-in 0.143.0 />}}
52 : (`[]string`) The headers to extract from the server's response, accessible through the resource's [`Data.Headers`][] method. Header name matching is case-insensitive.
53
54 `timeout`
55 : {{< new-in 0.157.0 />}}
56 : (`string`) The duration after which the request is cancelled if it does not complete, expressed as a [duration](g). If not specified, the request will timeout after 2 minutes.
57
58 ## Options examples
59
60 > [!NOTE]
61 > For brevity, the examples below do not include [error handling](#error-handling).
62
63 To include a header:
64
65 ```go-html-template
66 {{ $url := "https://example.org/api" }}
67 {{ $opts := dict
68 "headers" (dict "Authorization" "Bearer abcd")
69 }}
70 {{ $resource := resources.GetRemote $url $opts }}
71 ```
72
73 To specify more than one value for the same header key, use a slice:
74
75 ```go-html-template
76 {{ $url := "https://example.org/api" }}
77 {{ $opts := dict
78 "headers" (dict "X-List" (slice "a" "b" "c"))
79 }}
80 {{ $resource := resources.GetRemote $url $opts }}
81 ```
82
83 To post data:
84
85 ```go-html-template
86 {{ $url := "https://example.org/api" }}
87 {{ $opts := dict
88 "method" "post"
89 "body" `{"complete": true}`
90 "headers" (dict "Content-Type" "application/json")
91 }}
92 {{ $resource := resources.GetRemote $url $opts }}
93 ```
94
95 To override the default cache key:
96
97 ```go-html-template
98 {{ $url := "https://example.org/images/a.jpg" }}
99 {{ $opts := dict
100 "key" (print $url (now.Format "2006-01-02"))
101 }}
102 {{ $resource := resources.GetRemote $url $opts }}
103 ```
104
105 To extract specific headers from the server's response:
106
107 ```go-html-template
108 {{ $url := "https://example.org/images/a.jpg" }}
109 {{ $opts := dict
110 "method" "HEAD"
111 "responseHeaders" (slice "X-Frame-Options" "Server")
112 }}
113 {{ $resource := resources.GetRemote $url $opts }}
114 ```
115
116 Use the `timeout` option to prevent slow external requests from stalling the build when fetching multiple remote feeds:
117
118 ```go-html-template
119 {{ $url := "https://example.org/feed.rss" }}
120 {{ $opts := dict "timeout" "10s" }}
121 {{ with try (resources.GetRemote $url $opts) }}
122 {{ with .Err }}
123 {{ warnf "Failed to fetch feed: %s" . }}
124 {{ else with .Value }}
125 {{ $data = . | transform.Unmarshal }}
126 {{ end }}
127 {{ end }}
128 ```
129
130 ## Remote data
131
132 When retrieving remote data, use the [`transform.Unmarshal`][] function to [unmarshal](g) the response.
133
134 ```go-html-template
135 {{ $data := dict }}
136 {{ $url := "https://example.org/books.json" }}
137 {{ with try (resources.GetRemote $url) }}
138 {{ with .Err }}
139 {{ errorf "%s" . }}
140 {{ else with .Value }}
141 {{ $data = . | transform.Unmarshal }}
142 {{ else }}
143 {{ errorf "Unable to get remote resource %q" $url }}
144 {{ end }}
145 {{ end }}
146 ```
147
148 > [!NOTE]
149 > 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`.
150 >
151 > 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:
152 >
153 > `{{ $data = .Content | transform.Unmarshal }}`
154
155 ## Error handling
156
157 Use the [`try`][] statement to capture HTTP request errors. If you do not handle the error yourself, Hugo will fail the build.
158
159 > [!NOTE]
160 > Hugo does not classify an HTTP response with status code 404 as an error. In this case `resources.GetRemote` returns `nil`.
161
162 ```go-html-template
163 {{ $url := "https://broken-example.org/images/a.jpg" }}
164 {{ with try (resources.GetRemote $url) }}
165 {{ with .Err }}
166 {{ errorf "%s" . }}
167 {{ else with .Value }}
168 <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
169 {{ else }}
170 {{ errorf "Unable to get remote resource %q" $url }}
171 {{ end }}
172 {{ end }}
173 ```
174
175 To log an error as a warning instead of an error:
176
177 ```go-html-template
178 {{ $url := "https://broken-example.org/images/a.jpg" }}
179 {{ with try (resources.GetRemote $url) }}
180 {{ with .Err }}
181 {{ warnf "%s" . }}
182 {{ else with .Value }}
183 <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
184 {{ else }}
185 {{ warnf "Unable to get remote resource %q" $url }}
186 {{ end }}
187 {{ end }}
188 ```
189
190 ## HTTP response
191
192 The [`Data`][] method on a resource returned by the `resources.GetRemote` function returns information from the HTTP response.
193
194 ## Caching
195
196 Resources returned from `resources.GetRemote` are cached to disk. See [configure file caches][] for details.
197
198 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.
199
200 ```go-html-template
201 {{ $url := "https://example.org/images/a.jpg" }}
202 {{ $cacheKey := print $url (now.Format "2006-01-02") }}
203 {{ $opts := dict "key" $cacheKey }}
204 {{ $resource := resources.GetRemote $url $opts }}
205 ```
206
207 ## Security
208
209 To protect against malicious intent, the `resources.GetRemote` function inspects the server response including:
210
211 - The [Content-Type][] in the response header
212 - The file extension, if any
213 - The content itself
214
215 If Hugo is unable to resolve the media type to an entry in its [allowlist][], the function throws an error:
216
217 ```text
218 ERROR error calling resources.GetRemote: failed to resolve media type...
219 ```
220
221 For example, you will see the error above if you attempt to download an executable.
222
223 Although the allowlist contains entries for common media types, you may encounter situations where Hugo is unable to resolve the media type of a file that you know to be safe. In these situations, edit your project configuration to add the media type to the allowlist. For example:
224
225 {{< code-toggle file=hugo >}}
226 [security.http]
227 mediaTypes = ['^application/vnd\.api\+json$']
228 {{< /code-toggle >}}
229
230 Note that the entry above is:
231
232 - An _addition_ to the allowlist; it does not _replace_ the allowlist
233 - An array of [regular expressions](g)
234
235 [Content-Type]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type
236 [`Data.Headers`]: /methods/resource/data/#headers
237 [`Data`]: /methods/resource/data/
238 [`Permalink`]: /methods/resource/permalink/
239 [`Publish`]: /methods/resource/publish/
240 [`RelPermalink`]: /methods/resource/relpermalink/
241 [`publishDir`]: /configuration/all/#publishdir
242 [`transform.Unmarshal`]: /functions/transform/unmarshal/
243 [`try`]: /functions/go-template/try/
244 [allowlist]: https://en.wikipedia.org/wiki/Whitelist
245 [configure file caches]: /configuration/caches/