Multiple Content Types
Return text, JSON, XML, and YAML from different routes.
Prerequisites: see Introduction.
Full source
File: pwsh/tutorial/examples/2.1-Multiple-Content-Types.ps1
<#
Sample Kestrun Server Configuration with Multiple Content Types
This script demonstrates how to set up a simple Kestrun server with multiple routes.
The server will respond with different content types based on the requested route.
FileName: 2.1-Multiple-Content-Types.ps1
#>
param(
[int]$Port = 5000,
[IPAddress]$IPAddress = [IPAddress]::Loopback
)
# Create a new Kestrun server
New-KrServer -Name 'Simple Server'
# Add a listener on the configured port and IP address
Add-KrEndpoint -Port $Port -IPAddress $IPAddress
# Enable Kestrun configuration
Enable-KrConfiguration
# Map the route
Add-KrMapRoute -Verbs Get -Path '/hello' -ScriptBlock {
Write-KrTextResponse -InputObject 'Hello, World!' -StatusCode 200
}
# Map the route for JSON response
Add-KrMapRoute -Verbs Get -Path '/hello-json' -ScriptBlock {
Write-KrJsonResponse -InputObject @{ message = 'Hello, World!' } -StatusCode 200
}
# Map the route for XML response
Add-KrMapRoute -Verbs Get -Path '/hello-xml' -ScriptBlock {
Write-KrXmlResponse -InputObject @{ message = 'Hello, World!' } -StatusCode 200
}
# Map the route for YAML response
Add-KrMapRoute -Verbs Get -Path '/hello-yaml' -ScriptBlock {
Write-KrYamlResponse -InputObject @{ message = 'Hello, World!' } -StatusCode 200
}
# Start the server asynchronously
Start-KrServer
Step-by-step
- Initialize root:
Initialize-KrRoot -Path $PSScriptRootso relative paths resolve predictably. - Create server and listener:
New-KrServer;Add-KrEndpoint -Port 5000 -IPAddress Loopback. - Apply config:
Enable-KrConfigurationcommits staged listeners and runtime. - Map routes and choose a content helper per route:
- GET
/hello→ Write-KrTextResponse (Content-Type: text/plain) - GET
/hello-json→ Write-KrJsonResponse (application/json) - GET
/hello-xml→ Write-KrXmlResponse (application/xml) - GET
/hello-yaml→ Write-KrYamlResponse (application/x-yaml) Each helper accepts optional parameters like-StatusCode, and some accept-Headers.
- GET
- Start server:
Start-KrServerbegins processing requests.
Try it
Save the sample locally so it’s easy to run. Copy the contents of pwsh/tutorial/examples/2.1-Multiple-Content-Types.ps1 into a new file in an empty working folder (for example, content-types.ps1), then run:
# From your working folder
pwsh .\content-types.ps1
curl http://127.0.0.1:5000/hello
curl http://127.0.0.1:5000/hello-json
curl http://127.0.0.1:5000/hello-xml
curl http://127.0.0.1:5000/hello-yaml
PowerShell alternatives:
Invoke-WebRequest -Uri 'http://127.0.0.1:5000/hello' | Select-Object -ExpandProperty Content
Invoke-RestMethod -Uri 'http://127.0.0.1:5000/hello-json' # auto-parses JSON
Invoke-WebRequest -Uri 'http://127.0.0.1:5000/hello-xml' | Select-Object -ExpandProperty Content
Invoke-WebRequest -Uri 'http://127.0.0.1:5000/hello-yaml' | Select-Object -ExpandProperty Content
References
- Write-KrTextResponse
- Write-KrJsonResponse
- Write-KrXmlResponse
- Write-KrYamlResponse
- Add-KrMapRoute
- Start-KrServer
Troubleshooting
- JSON not parsed by Invoke‑WebRequest: prefer Invoke‑RestMethod for JSON content.
- YAML shown as raw text: that’s expected; PowerShell doesn’t auto‑parse YAML by default.
Previous / Next
Previous: Hello World Next: Multi-Language-Routes