Structured (XML / YAML / CSV)
Return alternative serializations for interoperability, configuration and exports.
Full source
File: pwsh/tutorial/examples/9.2-Structured-Xml-Yaml-Csv.ps1
<#
Sample: Structured Formats (XML / YAML / CSV)
Purpose: Demonstrate XML, YAML, and CSV responses in a Kestrun server.
File: 9.2-Structured-Xml-Yaml-Csv.ps1
Notes: Shows alternative serialization formats for objects.
#>
param(
[int]$Port = 5000,
[IPAddress]$IPAddress = [IPAddress]::Loopback
)
# 1. Logging
New-KrLogger | Add-KrSinkConsole | Register-KrLogger -Name 'console' -SetAsDefault
# 2. Server
New-KrServer -Name 'Responses 9.2'
# 3. Listener
Add-KrEndpoint -IPAddress $IPAddress -Port $Port
# 4. Runtime
# Finalize configuration
Enable-KrConfiguration
# XML route
Add-KrMapRoute -Pattern '/xml' -Verbs GET -ScriptBlock {
Write-KrXmlResponse -InputObject @{ Id = 1; Name = 'Alpha'; Nested = @{ X = 10; Y = 20 } }
}
# YAML route
Add-KrMapRoute -Pattern '/yaml' -Verbs GET -ScriptBlock {
@{ env = 'dev'; enabled = $true; tags = @('one', 'two') },
@{ Id = 1; Name = 'Alpha'; Nested = @{ X = 10; Y = 20 } } | Write-KrYamlResponse -ContentType 'application/x-yaml'
}
# CSV route
Add-KrMapRoute -Pattern '/csv' -Verbs GET -ScriptBlock {
$data = 1..5 | ForEach-Object { @{ Id = $_; Square = ($_ * $_) } }
Write-KrCsvResponse -InputObject $data
}
# Start the server
Start-KrServer -CloseLogsOnExit
Step-by-step
- Logging: Register console logger as default.
- Server: Create server named ‘Responses 9.2’.
- Listener: Listen on 127.0.0.1:5000.
/xml: serialize object with nested property to XML./yaml: serialize object with array property to YAML./csv: build collection, output CSV text.- Enable configuration and start server.
Try it
curl http://127.0.0.1:5000/xml
curl http://127.0.0.1:5000/yaml
curl http://127.0.0.1:5000/csv
PowerShell equivalents:
Invoke-RestMethod -Uri http://127.0.0.1:5000/xml
Invoke-RestMethod -Uri http://127.0.0.1:5000/yaml
Invoke-WebRequest -Uri http://127.0.0.1:5000/csv | Select -ExpandProperty Content
Example routes
# Logging
New-KrLogger | Add-KrSinkConsole | Register-KrLogger -Name 'console' -SetAsDefault
# Server
New-KrServer -Name 'Responses 9.2'
# Listener
Add-KrEndpoint -IPAddress '127.0.0.1' -Port 5000
# Enable configuration
Enable-KrConfiguration
# XML route
Add-KrMapRoute -Pattern '/xml' -Verbs GET -ScriptBlock {
@{ Id = 1; Name = 'Alpha'; Nested = @{ X = 10; Y = 20 } } | Write-KrXmlResponse -ContentType 'application/xml'
}
# YAML route
Add-KrMapRoute -Pattern '/yaml' -Verbs GET -ScriptBlock {
@{ env = 'dev'; enabled = $true; tags = @('one','two') } | Write-KrYamlResponse -ContentType 'application/x-yaml'
}
# CSV route
Add-KrMapRoute -Pattern '/csv' -Verbs GET -ScriptBlock {
$data = 1..5 | ForEach-Object { @{ Id = $_; Square = ($_ * $_) } }
$data | Write-KrCsvResponse -ContentType 'text/csv'
}
# Start the server
Start-KrServer
Tips
- Prefer JSON for public APIs; use XML/YAML when clients require it.
- For very large tabular exports consider streaming (see Binary & Stream chapter).
Troubleshooting
| Symptom | Cause | Fix |
|---|---|---|
| Wrong content type | Default type overridden | Pass explicit -ContentType |
| YAML not formatted | Input not object/hashtable | Ensure proper object is piped |
| CSV missing headers | Inconsistent properties | Use uniform hashtables/objects |
References
Previous / Next
Previous: Text & JSON Next: Binary & Stream