Skip to content

Commit

Permalink
Merge pull request #1 from Badgerati/develop
Browse files Browse the repository at this point in the history
v1.0.0
  • Loading branch information
Badgerati authored Dec 29, 2019
2 parents bd7e1e4 + 1f10952 commit 81bd7d2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
52 changes: 49 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,69 @@ or you can let Pode install it for you locally, by adding the following into you

### Body Parser

You'll need to first import the module, and then enable it as a body-parser within your server's scriptblock:
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:

```powershell
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:

```yaml
---
name: bob
```
Then you can access the `name` in a Route via the event parameters `Data` object:

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

Or if the request had multiple documents:

```yaml
---
name: bob
---
name: bill
```

Then the second `name` is accessible as follows:

```powershell
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:

```powershell
Add-PodeRoute -Method Get -Path '/' -ScriptBlock {
Write-PodeYamlResponse -Value @{ Name = 'example' }
Write-PodeYamlResponse -Value @{ Name = 'bob' }
}
```

This will write back to the client the follow payload:

```yaml
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.
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.
1 change: 1 addition & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"start": "./rest-api-yaml.ps1"
},
"modules": {
"powershell-yaml": "latest",
"Pode.Yaml": "1.0.0"
},
"author": "Matthew Kelly (Badgerati)",
Expand Down
2 changes: 1 addition & 1 deletion src/Pode.Yaml.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
Copyright = 'Copyright (c) 2019 Matthew Kelly (Badgerati), licensed under the MIT License.'

# Description of the functionality provided by this module
Description = 'A YAML Requesta dn Response extension module for Pode'
Description = 'A YAML Request and Response parser extension module for the Pode web server (v1.3.0+)'

# Minimum version of the Windows PowerShell engine required by this module
PowerShellVersion = '5.0'
Expand Down

0 comments on commit 81bd7d2

Please sign in to comment.