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