REST methods - more docs


#1

Hi!

I’m working on multiple REST endpoints and it looks to me like behavior is different across all of them. Would it be possible to document these differences?

E.g. I have a put endpoint:

New-UDEndpoint -Url /dns/cname -Method PUT -Endpoint {
    param (
        [Parameter(Mandatory)]
        [String]$Name,
        [Parameter(Mandatory)]
        [String]$Target,
        [String]$Zone = 'optiver.com'
    )
    # Some code goes here
    }
} -AuthorizationPolicy DnsAdmin

When I try to call it (both with -Body and with just uri encoded parameters) I get error:

A parameter cannot be found that matches parameter name 'body'.

With some more docs I should be able to understand what I’m missing here. ATM it’s more trial and erorr… :wink:


#2

Hmmm strange. I’ll have to futz with that.

As for the docs go, I can work on this particular section a bit more though, it really should be consistent between verbs.


#3

Hi Adam

Looks like behavior changes when CmdletBinding is involved. For example that works:

$putWithUri = New-UDEndpoint -Url /uri -Endpoint {
    param (
        $Param
    )

    $PSBoundParameters | ConvertTo-JsonEx
} -Method PUT -AuthorizationPolicy Windows

$putwithColon = New-UDEndpoint -Url /colon/:param -Endpoint {
    param (
        $Param
    )

    $PSBoundParameters | ConvertTo-JsonEx
} -Method PUT -AuthorizationPolicy Windows

But that one will return info about body not being defined:

$putWithUri = New-UDEndpoint -Url /uri -Endpoint {
    param (
        [Parameter(Mandatory)]
        $Param
    )

    $PSBoundParameters | ConvertTo-JsonEx
} -Method PUT -AuthorizationPolicy Windows

$putwithColon = New-UDEndpoint -Url /colon/:param -Endpoint {
    param (
        [Parameter(Mandatory)]
        $Param
    )

    $PSBoundParameters | ConvertTo-JsonEx
} -Method PUT -AuthorizationPolicy Windows

I suspect the problem here is that you pass Body as a parameter always, thus CmdletBinding is choking on it. Adding it as a parameter to definition of the endpoint (even if I prefer more direct access to parameters) hides the problem:


irm -Method Put -Uri http://localhost:88/api/colon/foo -UseDefaultCredentials

Body Param
---- -----
     foo  

#4

OK, looks like this might be a call-related, not method-related. Today I hit similar problem with POST.

If I call my post like this:

irm 'https://ud.my.domain/api/dns/cnameswap?name=foo&target=hostname.domain' -UseDefaultCredentials -Method Post

I get exactly same error:

error
-----
@{message=A parameter cannot be found that matches parameter name 'body'.; location=; type=error; id=; refreshInterval=0; autoRefresh=False; hasCallback=Fa...

However, if I call exactly same API with this approach:

irm 'https://ud.my.domain/api/dns/cnameswap' -Body @{ Name = 'foo'; target = 'hostname.domain' } -UseDefaultCredentials -Method Post

… it all works fine. So it looks like if parameters are in the Uri, $Body is always passed to Post/Put and fails if param block on the endpoint has any [Parameter()] making whole thing use CmdletBinding and has no $Body parameter defined…