Badgerati / Pode

Pode is a Cross-Platform PowerShell web framework for creating REST APIs, Web Sites, and TCP/SMTP servers
https://badgerati.github.io/Pode
MIT License
844 stars 91 forks source link

PATCH or PUT route and $Webevent.Data is null #1410

Closed ChrisLynchHPE closed 5 hours ago

ChrisLynchHPE commented 5 hours ago

Question

I'm building an API service for my lab, and I'd like to implement either a PATCH or PUT call to modify an internal object for tracking purposes. Essentially the object is one large Hashtable. In my API Patch or Put call, I want to insert a new entry to this Hashtable, which I'll use the .Add(key, value) method. Well, when testing this in operation, the $Webevent.Data property is empty. Here is an example:

    Add-PodeRoute -Method Put, Patch, Post -Path '/api/endpoint' -ScriptBlock {

        Write-PodeJsonResponse -Value @{
            Name = $WebEvent.Data.name
            Value = $WebEvent.Data.value
        }
    }

To invoke this, I'm using the following Invoke-RestMethod call:

# PATCH call
Invoke-RestMethod -uri http://localhost:8080/api/endpoint -Method Patch -Body @{name = "name1"; value = "value1"}

# Returns:

Name Value
---- -----

# PUT call
Invoke-RestMethod -uri http://localhost:8080/api/endpoint -Method Put -Body @{name = "name1"; value = "value1"}

# Returns:

Name Value
---- -----

# POST call
Invoke-RestMethod -uri http://localhost:8080/api/endpoint -Method Post -Body @{name = "name1"; value = "value1"}

# Returns:

Name  Value
----  -----
name1 value1

When I examine $Webevent when either PATCH or PUT is called, Data is empty:

Name                           Value
----                           -----
TransferEncoding
Auth                           {}
Metadata                       {}
Files                          {}
ContentType
Path                           /api/endpoint
StaticContent
Route                          {[Endpoint, System.Collections.Hashtable], [Middleware, System.Object[]], [Metrics, System.Collections.Hashtable], [ErrorType, ]…}
Cookies                        {}
Sse
Timestamp                      10/9/2024 9:25:39 PM
Data                           {}
ErrorType
OnEnd                          {}
PendingCookies                 {}
Request                        Pode.PodeHttpRequest
Lockable                       {}
Response                       Pode.PodeResponse
Ranges
Endpoint                       {[Address, localhost:8080], [Protocol, http], [Name, a6bb3005-e104-959d-7571-5c5db4ae6008]}
Query                          {}
Method                         patch
Parameters                     {[0, /api/endpoint]}
Streamed                       True
AcceptEncoding

Is this a bug? Am I using this HTTP method correctly?

PS version: 7.4.5 Pode version: 2.11.0

ChrisLynchHPE commented 5 hours ago

Okay, after a bit more testing, it appears that my Invoke-RestMethod was not right. Invoke-RestMethod isn't converting the body from Hashtable to JSON, and I need to then also pass -ContenType "application/json". So, this call achieves what I need:

Invoke-RestMethod -uri http://localhost:8080/api/endpoint -Method Patch -Body (@{name = "name1"; value = "value1"} | ConvertTo-Json) -ContentType "application/json"

# Returns:

Name  Value
----  -----
name1 value1

I'll implement $Webevent.ContentType validation in the route I'm adding to ensure the caller includes that correctly. This API endpoint will only process and handle JSON content, nothing else.