Skip to content

Commit 1cd17ad

Browse files
authored
feat: add custom metadata support and HTTP response metadata (#2051)
* . * . * . * . * . * . * . * . * Add --allow-errors to http status check example
1 parent f62142d commit 1cd17ad

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

book/metadata.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,22 @@ metadata (open Cargo.toml) | get span
3737
```
3838

3939
The span "start" and "end" here refer to where the underline will be in the line. If you count over 5, and then count up to 15, you'll see it lines up with the "Cargo.toml" filename. This is how the error we saw earlier knew what to underline.
40+
41+
## Custom Metadata
42+
43+
You can attach arbitrary metadata to pipeline data using the [`metadata set`](/commands/docs/metadata_set.md) command with the `--merge` flag:
44+
45+
```nu
46+
"data" | metadata set --merge {custom_key: "custom_value"}
47+
```
48+
49+
## HTTP Response Metadata
50+
51+
All HTTP commands attach response metadata:
52+
53+
```nu
54+
http get https://api.example.com | metadata | get http_response.status
55+
# => 200
56+
```
57+
58+
For working with metadata while streaming response bodies, see the [HTTP cookbook](/cookbook/http.html#accessing-http-response-metadata-while-streaming).

cookbook/http.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ To upload a form with a file (think a common file upload form in a browser, wher
186186

187187
1. Specify the content type as `multipart/form-data`
188188
2. Provide the record as the POST body
189-
3. Provide the file data in one of the record fields as *binary* data.
189+
3. Provide the file data in one of the record fields as _binary_ data.
190190

191191
```nu
192192
http post https://httpbin.org/post --content-type "multipart/form-data" {
@@ -255,3 +255,40 @@ http post https://httpbin.org/post --content-type "multipart/form-data" {
255255
# => │ url │ https://httpbin.org/post │
256256
# => ╰─────────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────╯
257257
```
258+
259+
---
260+
261+
### Accessing HTTP Response Metadata While Streaming
262+
263+
All HTTP commands attach response metadata. To access it after the response completes:
264+
265+
```nu
266+
http get https://api.example.com/data.json | metadata | get http_response.status
267+
# => 200
268+
```
269+
270+
To work with metadata while streaming the response body, use `metadata access`:
271+
272+
```nu
273+
# Log status and headers while streaming a large JSONL file
274+
http get --allow-errors https://api.example.com/events.jsonl
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+
if $meta.http_response.status != 200 {
280+
error make {msg: $"Failed with status ($meta.http_response.status)"}
281+
} else { }
282+
}
283+
| lines
284+
| each { from json }
285+
| where event_type == "error"
286+
```
287+
288+
The response body streams through the pipeline while you inspect metadata and process the stream simultaneously. Before `metadata access`, you needed `--full` to get metadata, which consumed the entire body and prevented streaming.
289+
290+
Available metadata:
291+
292+
- `status` - HTTP status code (200, 404, 500, etc.)
293+
- `headers` - `[{name, value}, ...]`
294+
- `urls` - Redirect history

0 commit comments

Comments
 (0)