Skip to content

Commit 5dc76d8

Browse files
committed
Document the responseHeaders option for resources.GetRemote
1 parent 53c7627 commit 5dc76d8

File tree

2 files changed

+81
-47
lines changed

2 files changed

+81
-47
lines changed

content/en/functions/resources/GetRemote.md

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,39 @@ toc: true
3535

3636
The `resources.GetRemote` function takes an optional map of options.
3737

38+
###### body
39+
40+
(`string`) The data you want to transmit to the server.
41+
42+
###### headers
43+
44+
(`map[string][]string`) The collection of key-value pairs that provide additional information about the request.
45+
46+
###### key
47+
48+
(`string`) The cache key. Hugo derives the default value from the URL and options map. See [caching](#caching).
49+
50+
###### method
51+
52+
(`string`) The action to perform on the requested resource, typically one of `GET`, `POST`, or `HEAD`.
53+
54+
###### responseHeaders
55+
{{< new-in 0.143.0 >}}
56+
57+
(`[]string`) The headers to extract from the server's response, accessible through the resource's [`Data.Headers`] method. Header name matching is case-insensitive.
58+
59+
[`Data.Headers`]: /methods/resource/data/#headers
60+
61+
## Options examples
62+
63+
{{% note %}}
64+
For brevity, the examples below do not include [error handling].
65+
66+
[error handling]: #error-handling
67+
{{% /note %}}
68+
69+
To include a header:
70+
3871
```go-html-template
3972
{{ $url := "https://example.org/api" }}
4073
{{ $opts := dict
@@ -43,7 +76,7 @@ The `resources.GetRemote` function takes an optional map of options.
4376
{{ $resource := resources.GetRemote $url $opts }}
4477
```
4578

46-
If you need multiple values for the same header key, use a slice:
79+
To specify more than one value for the same header key, use a slice:
4780

4881
```go-html-template
4982
{{ $url := "https://example.org/api" }}
@@ -53,7 +86,7 @@ If you need multiple values for the same header key, use a slice:
5386
{{ $resource := resources.GetRemote $url $opts }}
5487
```
5588

56-
You can also change the request method and set the request body:
89+
To post data:
5790

5891
```go-html-template
5992
{{ $url := "https://example.org/api" }}
@@ -65,6 +98,27 @@ You can also change the request method and set the request body:
6598
{{ $resource := resources.GetRemote $url $opts }}
6699
```
67100

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+
68122
## Remote data
69123

70124
When retrieving remote data, use the [`transform.Unmarshal`] function to [unmarshal](g) the response.
@@ -139,40 +193,6 @@ The [`Data`] method on a resource returned by the `resources.GetRemote` function
139193

140194
[`Data`]: /methods/resource/data/
141195

142-
```go-html-template
143-
{{ $url := "https://example.org/images/a.jpg" }}
144-
{{ with try (resources.GetRemote $url) }}
145-
{{ with .Err }}
146-
{{ errorf "%s" . }}
147-
{{ else with .Value }}
148-
{{ with .Data }}
149-
{{ .ContentLength }} → 42764
150-
{{ .ContentType }} → image/jpeg
151-
{{ .Status }} → 200 OK
152-
{{ .StatusCode }} → 200
153-
{{ .TransferEncoding }} → []
154-
{{ end }}
155-
{{ else }}
156-
{{ errorf "Unable to get remote resource %q" $url }}
157-
{{ end }}
158-
{{ end }}
159-
```
160-
161-
ContentLength
162-
: (`int`) The content length in bytes.
163-
164-
ContentType
165-
: (`string`) The content type.
166-
167-
Status
168-
: (`string`) The HTTP status text.
169-
170-
StatusCode
171-
: (`int`) The HTTP status code.
172-
173-
TransferEncoding
174-
: (`string`) The transfer encoding.
175-
176196
## Caching
177197

178198
Resources returned from `resources.GetRemote` are cached to disk. See [configure file caches] for details.
@@ -184,7 +204,8 @@ Override the cache key by setting a `key` in the options map. Use this approach
184204
```go-html-template
185205
{{ $url := "https://example.org/images/a.jpg" }}
186206
{{ $cacheKey := print $url (now.Format "2006-01-02") }}
187-
{{ $resource := resources.GetRemote $url (dict "key" $cacheKey) }}
207+
{{ $opts := dict "key" $cacheKey }}
208+
{{ $resource := resources.GetRemote $url $opts }}
188209
```
189210

190211
[configure file caches]: /getting-started/configuration/#configure-file-caches

content/en/methods/resource/Data.md

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ The `Data` method on a resource returned by the [`resources.GetRemote`] function
1717

1818
```go-html-template
1919
{{ $url := "https://example.org/images/a.jpg" }}
20+
{{ $opts := dict "responseHeaders" (slice "Server") }}
2021
{{ with try (resources.GetRemote $url) }}
2122
{{ with .Err }}
2223
{{ errorf "%s" . }}
2324
{{ else with .Value }}
2425
{{ with .Data }}
2526
{{ .ContentLength }} → 42764
2627
{{ .ContentType }} → image/jpeg
28+
{{ .Headers }} → map[Server:[Netlify]]
2729
{{ .Status }} → 200 OK
2830
{{ .StatusCode }} → 200
2931
{{ .TransferEncoding }} → []
@@ -34,19 +36,30 @@ The `Data` method on a resource returned by the [`resources.GetRemote`] function
3436
{{ end }}
3537
```
3638

37-
ContentLength
38-
: (`int`) The content length in bytes.
39+
###### ContentLength
3940

40-
ContentType
41-
: (`string`) The content type.
41+
(`int`) The content length in bytes.
4242

43-
Status
44-
: (`string`) The HTTP status text.
43+
###### ContentType
4544

46-
StatusCode
47-
: (`int`) The HTTP status code.
45+
(`string`) The content type.
4846

49-
TransferEncoding
50-
: (`string`) The transfer encoding.
47+
###### Headers
48+
49+
(`map[string][]string`) A map of response headers matching those requested in the [`responseHeaders`] option passed to the `resources.GetRemote` function. The header name matching is case-insensitive. In most cases there will be one value per header key.
50+
51+
[`responseHeaders`]: /functions/resources/getremote/#responseheaders
52+
53+
###### Status
54+
55+
(`string`) The HTTP status text.
56+
57+
###### StatusCode
58+
59+
(`int`) The HTTP status code.
60+
61+
###### TransferEncoding
62+
63+
(`string`) The transfer encoding.
5164

5265
[`resources.GetRemote`]: /functions/resources/getremote/

0 commit comments

Comments
 (0)