1

I am running Invoke-Webrequest to upload a file to Nexus server the first time when I run it, works fine but when I re-run it, fails because the file already exists and Nexus is set to not allowing overwriting existing files. What I need is that when I run Invoke-Webrequest when the file I'm trying to upload already exists, it just simply returns 400 bad request. How can I get more detailed error message so that I know what the problem is with the request?

try {
    // invoke-webrequest
}
catch {
    throw $_.Exception.Response
}

1 Answers1

0

i guess this will help you quite a lot, i find it in this Answer:

function ParseErrorForResponseBody($Error) {
    if ($PSVersionTable.PSVersion.Major -lt 6) {
        if ($Error.Exception.Response) {  
            $Reader = New-Object System.IO.StreamReader($Error.Exception.Response.GetResponseStream())
            $Reader.BaseStream.Position = 0
            $Reader.DiscardBufferedData()
            $ResponseBody = $Reader.ReadToEnd()
            if ($ResponseBody.StartsWith('{')) {
                $ResponseBody = $ResponseBody | ConvertFrom-Json
            }
            return $ResponseBody
        }
    }
    else {
        return $Error.ErrorDetails.Message
    }
}

try {
    $result = Invoke-WebRequest ...
}
catch {
    ParseErrorForResponseBody($_)
}
natiT
  • 1