Hi!
I’m trying to use REST APIs in Universal and thus far it was not great experience.
Compared to UD - it feels like it went back few steps.
- adding param block is breaking endpoint, not sure if that is intended, but if so - it seems like a pure design choice to me. I want to be able to explicitly configure parameters on my endpoints
- error handling feels completely broken: exception that are not handled are eaten up, New-PSUApiResponse would send StatusCode over but whole body is missing
Example endpoints.ps1 that should highlight basics:
New-PSUEndpoint -Url /broken -Endpoint {
New-PSUApiResponse -StatusCode 404 -Body 'Failed!'
} -Role Reader
New-PSUEndpoint -Url /params/:test -Endpoint {
Param (
[String]$Test
)
@{
test = $Test
} | ConvertTo-JsonEx
} -Role Reader
New-PSUEndpoint -Url /noparams/:test -Endpoint {
@{
test = $Test
} | ConvertTo-JsonEx
} -Role Reader
And results when I try to call both:
# calling /broken
try { irm http://psu-test/broken -used } catch { $_ | fl * -Force }
# result for /broken
PSMessageDetails :
Exception : System.Net.WebException: The remote server returned an error: (404) Not Found.
at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.GetResponse(WebRequest request)
at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.ProcessRecord()
TargetObject : System.Net.HttpWebRequest
CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {}
# calling /params
irm http://psu-test/params/myTest -UseDefaultCredentials
# result for /params
test
----
# calling /noparams
irm http://psu-test/noparams/myTest -UseDefaultCredentials
# result for /noparams
test
----
myTest
For us both parameters definition on even more importantly proper error handling are crucial elements of the puzzle…