0

I have an Azure Python function:

def main(req: func.HttpRequest) -> func.HttpResponse:
    return func.HttpResponse('TEST ERROR MESSAGE', status_code=403)

When I call the function through Powershell, I only receive a generic 403 error message: "The remote server returned an error: (403) Forbidden. I cannot extract the message "TEST ERROR MESSAGE" and I'm not sure why. Any help would be appreciated, I cannot find anything online. Is the error in my PowerShell or my Python? I am making the request and capturing the exception below:

try
{$response = Invoke-WebRequest -URI "https/path/to/func" -Method Get -CertificateThumbprint $Thumb -ErrorAction stop}
catch
{$response = $_.Exception}

If I set the status_code to 200 however, I can see the message "TEST ERROR MESSAGE" fine

Aegir
  • 117
  • 11

1 Answers1

0

I would try and replicate the issue with Invoke-RestMethod rather than Invoke-WebRequest

There have also been significant improvements in PowerShell v6+ with the Invoke-WebRequest and Invoke-RestMethod so if you are still using v5.1 then you will have to do a bit more work to get at the error response code.

https://stackoverflow.com/a/19122582/12040634

BrettMiller
  • 946
  • 2
  • 5
  • 12
  • Hi Brett, thanks for the response. Yep still using 5.1, can you give any information on the extra work needed? For example, Im using the method suggested in the link you pasted but still getting a generic error description, not the custom message I actually returned – Aegir Feb 24 '21 at 08:28
  • Hi Aegir, I would try the below and see if you can access the error that way. ```powershell $response = try { Invoke-RestMethod -Uri $uri -Method Get } catch { $_ } ``` Then accessing `$response.ErrorDetails` from there. I don't have a 5.1 terminal to test from unfortunately but if possible I would definitely recommend upgrading to PowerShell 7 to make use of the improvements and having the ability to install side by side is great. – BrettMiller Feb 24 '21 at 21:48