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
+19Lines changed: 19 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -37,3 +37,22 @@ metadata (open Cargo.toml) | get span
37
37
```
38
38
39
39
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).
### 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.)
0 commit comments