You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
All HTTP commands attach response metadata (status, headers, redirect history):
60
60
61
61
```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
68
64
```
69
65
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):
76
67
77
68
```nu
78
69
http get https://api.example.com/large-file
79
70
| metadata access {|meta|
71
+
print $"Status: ($meta.http_response.status)"
80
72
if $meta.http_response.status != 200 {
81
73
error make {msg: "Request failed"}
82
74
} else { }
83
75
}
84
-
| lines # body streams through only if status is 200
76
+
| lines # body streams through
85
77
| each {|line| process $line }
86
78
```
87
79
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}, ...]`
Copy file name to clipboardExpand all lines: cookbook/http.md
+14-12Lines changed: 14 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -258,32 +258,34 @@ http post https://httpbin.org/post --content-type "multipart/form-data" {
258
258
259
259
---
260
260
261
-
### Accessing HTTP Response Metadata
261
+
### Accessing HTTP Response Metadata While Streaming
262
262
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:
264
264
265
265
```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
270
267
# => 200
271
268
```
272
269
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:
274
271
275
272
```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
278
275
| 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
+
279
279
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)"}
281
281
} else { }
282
282
}
283
-
| save large-file.bin # only runs if status is 200
283
+
| lines
284
+
| from csv
285
+
| where amount > 1000
284
286
```
285
287
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.
287
289
288
290
Available metadata:
289
291
-`status` - HTTP status code (200, 404, 500, etc.)
0 commit comments