This is an extension module for the Pode web server (v1.3.0+). It allows you to parse and send YAML requests and responses.
Note: this module has a dependency on the powershell-yaml module
You can either install this module globally:
Install-Module -Name powershell-yaml
Install-Module -Name Pode.Yamlor you can let Pode install it for you locally, by adding the following into your package.json:
"modules": {
"powershell-yaml": "latest",
"pode.yaml": "latest"
}You can set this module to be used to parse Request payloads, that have relevant Content-Type. You'll need to first import the module, and then enable it as a body-parser within your server's scriptblock:
Import-PodeModule -Name Pode.Yaml -Now
Enable-PodeYamlBodyParserWhen the parser runs it will either set the event's Data object to be:
- A single hashtable - if one YAML document is supplied
- An array of hashtables - if multiple YAML documents are supplied
For example, if a Request comes in with the following payload:
---
name: bobThen you can access the name in a Route via the event parameters Data object:
Add-PodeRoute -Method Get -Path '/' -ScriptBlock {
param($e)
$e.Data.name | Out-PodeHost
}Or if the request had multiple documents:
---
name: bob
---
name: billThen the second name is accessible as follows:
Add-PodeRoute -Method Get -Path '/' -ScriptBlock {
param($e)
$e.Data[0].name | Out-PodeHost
}You can respond back with YAML in a Route as follows:
Add-PodeRoute -Method Get -Path '/' -ScriptBlock {
Write-PodeYamlResponse -Value @{ Name = 'bob' }
}This will write back to the client the follow payload:
name: bobThe default Content-Type expected on the request, and used on the response is application/x-yaml. You can change this by specifying the -ContentType parameter on the above PodeYaml functions.