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
Copy file name to clipboardExpand all lines: book/metadata.md
+18-5Lines changed: 18 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -56,20 +56,33 @@ This naming convention helps ensure different parts of the system don't overwrit
56
56
57
57
## HTTP Response Metadata
58
58
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.
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:
62
76
63
77
```nu
64
-
# Check status code while streaming the response
65
78
http get https://api.example.com/large-file
66
79
| metadata access {|meta|
67
80
if $meta.http_response.status != 200 {
68
81
error make {msg: "Request failed"}
69
82
} else { }
70
83
}
71
-
| lines
84
+
| lines # body streams through only if status is 200
72
85
| each {|line| process $line }
73
86
```
74
87
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.
Copy file name to clipboardExpand all lines: cookbook/http.md
+20-15Lines changed: 20 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -258,29 +258,34 @@ http post https://httpbin.org/post --content-type "multipart/form-data" {
258
258
259
259
---
260
260
261
-
### Checking HTTP Response Status While Streaming
261
+
### Accessing HTTP Response Metadata
262
262
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):
264
264
265
265
```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
268
278
| metadata access {|meta|
269
279
if $meta.http_response.status != 200 {
270
280
error make {msg: $"Request failed with status ($meta.http_response.status)"}
271
281
} else { }
272
282
}
273
-
| lines
274
-
| each {|line| process $line }
283
+
| save large-file.bin # only runs if status is 200
275
284
```
276
285
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.
282
287
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.)
0 commit comments