Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ChrisLynchHPE opened this issue Oct 9, 2024 · 1 comment
Closed

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

ChrisLynchHPE opened this issue Oct 9, 2024 · 1 comment

Comments

@ChrisLynchHPE
Copy link

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant