• This topic has 2 replies, 1 voice, and was last updated March 25, 2021 by Terry W.

Zerto API – Invoke-rest vs invoke-web

  • something that always confused me (and this is probably Zerto specific but I’ll throw it out here)

    I do all my requests using <strong>invoke-restmethod</strong> execpt 1, the inital <strong>invoke-webrequest</strong> to get my token

    for example

    $ZertoServer = 'zerto-zvm.internaldomain.local'
    $ZertoPort = "9669"
    $ZertoUser = "domain\Zerto-account"
    $ZertoPassword = "HLlmmt2W4rvcs3hlz8w"
    $BaseURL = "https://$($ZertoServer):$($ZertoPort)/v1"
    $AuthInfo = ("{0}:{1}" -f $ZertoUser, $ZertoPassword)
    $AuthInfo = [System.Text.Encoding]::UTF8.GetBytes($AuthInfo)
    $AuthInfo = [System.Convert]::ToBase64String($AuthInfo)
    $TokenSplat = @{
    Method = 'Post'
    SkipCertificateCheck = $true
    Uri = "$BaseURL/session/add"
    Headers = @{Authorization = ("Basic {0}" -f $AuthInfo) }
    Body = @{ "AuthenticationMethod" = "1" } | ConvertTo-Json
    ContentType = "application/JSON"
    # Why in gods green earth dosnt this work with invoke rest?
    $RestResponce = Invoke-RestMethod @TokenSplat
    $WebResponce = Invoke-WebRequest @TokenSplat
    $ZToken = @{"x-zerto-session" = "{0}" -f $WebResponce.headers.get_item("x-zerto-session") }
    $AlertsSpat = @{
    Headers = $ZToken
    Uri = "$BaseURL/alerts"
    Method = 'Get'
    SkipCertificateCheck = $true
    $AlertsJSON = Invoke-RestMethod @AlertsSpat

    so why does (using all the same details mind you)

    $RestResponce = Invoke-RestMethod @TokenSplat # Returns Nothing
    $WebResponce = Invoke-WebRequest @TokenSplat # Returns my session

    I see it in everyone’s examples, and its something I don’t understand why <strong>invoke-restmethod</strong> couldn’t achieve the same thing

    not sure what happened to my formatting, but it’s good enough now

    I notice I’ve put this in the wrong forum, should have been in the API/REST forum

    I cant seem to edit the post

    **Solved:** invoke-restmethod does not return the response headers by default invoke-webrequest does
    Capturing those headers in the variable fixes the problem (ps6/7 only likely), code below

    Invoke-RestMethod @TokenSplat -ResponseHeadersVariable RestSessinToken
    $ZToken = @{"x-zerto-session" = "{0}" -f $RestSessinToken.get_item("x-zerto-session") }
You must be logged in to create new topics. Click here to login