Skip to content

Commit 4fe711c

Browse files
committed
.
1 parent 92f4583 commit 4fe711c

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

book/metadata.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,33 @@ This naming convention helps ensure different parts of the system don't overwrit
5656

5757
## HTTP Response Metadata
5858

59-
All HTTP commands (`http get`, `http post`, etc.) automatically attach response metadata under the `"http_response"` key. This includes the status code, headers, and redirect history.
59+
All HTTP commands (`http get`, `http post`, etc.) attach response metadata:
6060

61-
You can access this metadata using the [`metadata access`](/commands/docs/metadata_access.md) command, which is especially useful for streaming large responses:
61+
```nu
62+
http get https://api.example.com | metadata | get http_response
63+
# => ╭─────────┬───────────────────╮
64+
# => │ status │ 200 │
65+
# => │ headers │ [list] │
66+
# => │ urls │ [list] │
67+
# => ╰─────────┴───────────────────╯
68+
```
69+
70+
The metadata includes:
71+
- `status` - HTTP status code (200, 404, 500, etc.)
72+
- `headers` - Response headers as `[{name, value}, ...]`
73+
- `urls` - Redirect history
74+
75+
For large responses, use [`metadata access`](/commands/docs/metadata_access.md) to check metadata before the body downloads:
6276

6377
```nu
64-
# Check status code while streaming the response
6578
http get https://api.example.com/large-file
6679
| metadata access {|meta|
6780
if $meta.http_response.status != 200 {
6881
error make {msg: "Request failed"}
6982
} else { }
7083
}
71-
| lines
84+
| lines # body streams through only if status is 200
7285
| each {|line| process $line }
7386
```
7487

75-
The `else { }` clause passes the input through when the check succeeds, allowing the response body to stream through the pipeline. This pattern lets you fail fast on error responses without downloading the entire body.
88+
The `else { }` passes the input through when the check succeeds.

cookbook/http.md

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -258,29 +258,34 @@ http post https://httpbin.org/post --content-type "multipart/form-data" {
258258

259259
---
260260

261-
### Checking HTTP Response Status While Streaming
261+
### Accessing HTTP Response Metadata
262262

263-
When working with large HTTP responses, you often want to stream the response body while still checking the HTTP status code. All HTTP commands automatically attach response metadata under the `"http_response"` key, which you can access using `metadata access`.
263+
All HTTP commands attach response metadata (status, headers, redirect history):
264264

265265
```nu
266-
# Stream a large response while checking the status code
267-
http get https://api.example.com/large-stream
266+
# After response completes
267+
http get https://api.example.com/data.json
268+
| metadata
269+
| get http_response.status
270+
# => 200
271+
```
272+
273+
For large responses, check status *before* downloading the body:
274+
275+
```nu
276+
# Fail fast without downloading body on error
277+
http get https://api.example.com/large-file.bin
268278
| metadata access {|meta|
269279
if $meta.http_response.status != 200 {
270280
error make {msg: $"Request failed with status ($meta.http_response.status)"}
271281
} else { }
272282
}
273-
| lines
274-
| each {|line| process $line }
283+
| save large-file.bin # only runs if status is 200
275284
```
276285

277-
The `metadata access` command allows you to inspect metadata before the response body streams through. The `else { }` clause is required to pass the input through when the status check succeeds. This pattern is useful for:
278-
279-
- Failing fast on error responses without downloading the entire body
280-
- Handling large payloads efficiently (the body still streams)
281-
- Checking response metadata without blocking the pipeline
286+
This checks metadata before the body streams through. If the status isn't 200, the error occurs immediately—the body is never downloaded.
282287

283-
The response metadata includes:
284-
- `status` - HTTP status code (e.g., 200, 404, 500)
285-
- `headers` - Response headers as a list of `{name, value}` records
286-
- `urls` - Redirect history (list of URLs followed)
288+
Available metadata:
289+
- `status` - HTTP status code (200, 404, 500, etc.)
290+
- `headers` - `[{name, value}, ...]`
291+
- `urls` - Redirect history

0 commit comments

Comments
 (0)