GetRemote.md - hugo - [fork] hugo port for 9front
 (HTM) git clone https://git.drkhsh.at/hugo.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Submodules
 (DIR) README
 (DIR) LICENSE
       ---
       GetRemote.md (7114B)
       ---
            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] example below.
           17 
           18 [`try`]: /functions/go-template/try
           19 [error handling]: #error-handling
           20 {{< /new-in >}}
           21 
           22 ```go-html-template
           23 {{ $url := "https://example.org/images/a.jpg" }}
           24 {{ with try (resources.GetRemote $url) }}
           25   {{ with .Err }}
           26     {{ errorf "%s" . }}
           27   {{ else with .Value }}
           28     <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
           29   {{ else }}
           30     {{ errorf "Unable to get remote resource %q" $url }}
           31   {{ end }}
           32 {{ end }}
           33 ```
           34 
           35 ## Options
           36 
           37 The `resources.GetRemote` function takes an optional map of options.
           38 
           39 ###### body
           40 
           41 (`string`) The data you want to transmit to the server.
           42 
           43 ###### headers
           44 
           45 (`map[string][]string`) The collection of key-value pairs that provide additional information about the request.
           46 
           47 ###### key
           48 
           49 (`string`) The cache key. Hugo derives the default value from the URL and options map. See [caching](#caching).
           50 
           51 ###### method
           52 
           53 (`string`) The action to perform on the requested resource, typically one of `GET`, `POST`, or `HEAD`.
           54 
           55 ###### responseHeaders
           56 {{< new-in 0.143.0 />}}
           57 
           58 (`[]string`) The headers to extract from the server's response, accessible through the resource's [`Data.Headers`] method. Header name matching is case-insensitive.
           59 
           60 [`Data.Headers`]: /methods/resource/data/#headers
           61 
           62 ## Options examples
           63 
           64 > [!note]
           65 > For brevity, the examples below do not include [error handling].
           66 
           67 [error handling]: #error-handling
           68 
           69 To include a header:
           70 
           71 ```go-html-template
           72 {{ $url := "https://example.org/api" }}
           73 {{ $opts := dict
           74   "headers" (dict "Authorization" "Bearer abcd")
           75 }}
           76 {{ $resource := resources.GetRemote $url $opts }}
           77 ```
           78 
           79 To specify more than one value for the same header key, use a slice:
           80 
           81 ```go-html-template
           82 {{ $url := "https://example.org/api" }}
           83 {{ $opts := dict
           84   "headers" (dict "X-List" (slice "a" "b" "c"))
           85 }}
           86 {{ $resource := resources.GetRemote $url $opts }}
           87 ```
           88 
           89 To post data:
           90 
           91 ```go-html-template
           92 {{ $url := "https://example.org/api" }}
           93 {{ $opts := dict
           94   "method" "post"
           95   "body" `{"complete": true}` 
           96   "headers" (dict  "Content-Type" "application/json")
           97 }}
           98 {{ $resource := resources.GetRemote $url $opts }}
           99 ```
          100 
          101 To override the default cache key:
          102 
          103 ```go-html-template
          104 {{ $url := "https://example.org/images/a.jpg" }}
          105 {{ $opts := dict 
          106   "key" (print $url (now.Format "2006-01-02"))
          107 }}
          108 {{ $resource := resources.GetRemote $url $opts }}
          109 ```
          110 
          111 To extract specific headers from the server's response:
          112 
          113 ```go-html-template
          114 {{ $url := "https://example.org/images/a.jpg" }}
          115 {{ $opts := dict
          116   "method" "HEAD"
          117   "responseHeaders" (slice "X-Frame-Options" "Server")
          118 }}
          119 {{ $resource := resources.GetRemote $url $opts }}
          120 ```
          121 
          122 ## Remote data
          123 
          124 When retrieving remote data, use the [`transform.Unmarshal`] function to [unmarshal](g) the response.
          125 
          126 [`transform.Unmarshal`]: /functions/transform/unmarshal/
          127 
          128 ```go-html-template
          129 {{ $data := dict }}
          130 {{ $url := "https://example.org/books.json" }}
          131 {{ with try (resources.GetRemote $url) }}
          132   {{ with .Err }}
          133     {{ errorf "%s" . }}
          134   {{ else with .Value }}
          135     {{ $data = . | transform.Unmarshal }}
          136   {{ else }}
          137     {{ errorf "Unable to get remote resource %q" $url }}
          138   {{ end }}
          139 {{ end }}
          140 ```
          141 
          142 > [!note]
          143 > 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`.
          144 >
          145 > 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:
          146 >
          147 > `{{ $data = .Content | transform.Unmarshal }}`
          148 
          149 ## Error handling
          150 
          151 Use the [`try`] statement to capture HTTP request errors. If you do not handle the error yourself, Hugo will fail the build.
          152 
          153 [`try`]: /functions/go-template/try
          154 
          155 > [!note]
          156 > Hugo does not classify an HTTP response with status code 404 as an error. In this case `resources.GetRemote` returns nil.
          157 
          158 ```go-html-template
          159 {{ $url := "https://broken-example.org/images/a.jpg" }}
          160 {{ with try (resources.GetRemote $url) }}
          161   {{ with .Err }}
          162     {{ errorf "%s" . }}
          163   {{ else with .Value }}
          164     <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
          165   {{ else }}
          166     {{ errorf "Unable to get remote resource %q" $url }}
          167   {{ end }}
          168 {{ end }}
          169 ```
          170 
          171 To log an error as a warning instead of an error:
          172 
          173 ```go-html-template
          174 {{ $url := "https://broken-example.org/images/a.jpg" }}
          175 {{ with try (resources.GetRemote $url) }}
          176   {{ with .Err }}
          177     {{ warnf "%s" . }}
          178   {{ else with .Value }}
          179     <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
          180   {{ else }}
          181     {{ warnf "Unable to get remote resource %q" $url }}
          182   {{ end }}
          183 {{ end }}
          184 ```
          185 
          186 ## HTTP response
          187 
          188 The [`Data`] method on a resource returned by the `resources.GetRemote` function returns information from the HTTP response.
          189 
          190 [`Data`]: /methods/resource/data/
          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 [configure file caches]: /configuration/caches/
          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 site configuration to add the media type to the allowlist. For example:
          224 
          225 {{< code-toggle file=hugo >}}
          226 [security.http]
          227 mediaTypes = ['^image/avif$','^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 [allowlist]: https://en.wikipedia.org/wiki/Whitelist
          236 [Content-Type]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type