Skip to content

Extension module for Pode, it allows you to parse and send YAML requests and responses

License

Notifications You must be signed in to change notification settings

Badgerati/Pode.Yaml

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pode YAML

MIT licensed Gitter PowerShell

This is an extension module for the Pode web server (v1.3.0+). It allows you to parse and send YAML requests and responses.

Install

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.Yaml

or you can let Pode install it for you locally, by adding the following into your package.json:

"modules": {
    "powershell-yaml": "latest",
    "pode.yaml": "latest"
}

Usage

Body Parser

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-PodeYamlBodyParser

When 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: bob

Then 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: bill

Then the second name is accessible as follows:

Add-PodeRoute -Method Get -Path '/' -ScriptBlock {
    param($e)
    $e.Data[0].name | Out-PodeHost
}

Response

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: bob

Content Type

The 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.