Skip to content

Commit 2e6d592

Browse files
committed
.
1 parent 4fe711c commit 2e6d592

File tree

2 files changed

+26
-27
lines changed

2 files changed

+26
-27
lines changed

book/metadata.md

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,33 +56,30 @@ 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.) attach response metadata:
59+
All HTTP commands attach response metadata (status, headers, redirect history):
6060

6161
```nu
62-
http get https://api.example.com | metadata | get http_response
63-
# => ╭─────────┬───────────────────╮
64-
# => │ status │ 200 │
65-
# => │ headers │ [list] │
66-
# => │ urls │ [list] │
67-
# => ╰─────────┴───────────────────╯
62+
http get https://api.example.com | metadata | get http_response.status
63+
# => 200
6864
```
6965

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:
66+
To work with metadata while streaming the response body, use [`metadata access`](/commands/docs/metadata_access.md):
7667

7768
```nu
7869
http get https://api.example.com/large-file
7970
| metadata access {|meta|
71+
print $"Status: ($meta.http_response.status)"
8072
if $meta.http_response.status != 200 {
8173
error make {msg: "Request failed"}
8274
} else { }
8375
}
84-
| lines # body streams through only if status is 200
76+
| lines # body streams through
8577
| each {|line| process $line }
8678
```
8779

88-
The `else { }` passes the input through when the check succeeds.
80+
Without `metadata access`, you'd need `--full` to get metadata, which consumes the entire response body and prevents streaming. With `metadata access`, the body continues streaming through the pipeline.
81+
82+
Metadata structure:
83+
- `status` - HTTP status code (200, 404, 500, etc.)
84+
- `headers` - Response headers as `[{name, value}, ...]`
85+
- `urls` - Redirect history

cookbook/http.md

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

259259
---
260260

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

263-
All HTTP commands attach response metadata (status, headers, redirect history):
263+
All HTTP commands attach response metadata. To access it after the response completes:
264264

265265
```nu
266-
# After response completes
267-
http get https://api.example.com/data.json
268-
| metadata
269-
| get http_response.status
266+
http get https://api.example.com/data.json | metadata | get http_response.status
270267
# => 200
271268
```
272269

273-
For large responses, check status *before* downloading the body:
270+
To work with metadata while streaming the response body, use `metadata access`. This is useful for handling unexpected responses (errors, redirects, content negotiation) without consuming the entire body:
274271

275272
```nu
276-
# Fail fast without downloading body on error
277-
http get https://api.example.com/large-file.bin
273+
# Log status and headers while streaming a large file
274+
http get https://api.example.com/large-dataset.csv
278275
| metadata access {|meta|
276+
print $"Status: ($meta.http_response.status)"
277+
print $"Content-Type: ($meta.http_response.headers | where name == content-type | get value.0)"
278+
279279
if $meta.http_response.status != 200 {
280-
error make {msg: $"Request failed with status ($meta.http_response.status)"}
280+
error make {msg: $"Failed with status ($meta.http_response.status)"}
281281
} else { }
282282
}
283-
| save large-file.bin # only runs if status is 200
283+
| lines
284+
| from csv
285+
| where amount > 1000
284286
```
285287

286-
This checks metadata before the body streams through. If the status isn't 200, the error occurs immediately—the body is never downloaded.
288+
The response body streams through the pipeline—you can inspect metadata and process the stream simultaneously. Before `metadata access`, you needed `--full` to get metadata, which consumed the entire body and prevented streaming.
287289

288290
Available metadata:
289291
- `status` - HTTP status code (200, 404, 500, etc.)

0 commit comments

Comments
 (0)