HTTPWebRequest error handling vs PS Invoke-WebRequest

jordi79jordi79 Member Posts: 277
Hi,
In Powershell, if you use Invoke-WebRequest, and if there is an error, you will get the full error message.
e.g.
Invoke-WebRequest : POST https://SOMEAPIURL with
My favourite address MODIFIED JN Street 100 JNStreet 200 Street 3 (if needed) Street 4 (if needed) Oslo 1234 NO true Mr. JN 93
: 400 Bad Request: This address already exists. To create a unique address, at least one of these fields must be changed: Name, Line 1, Line
2, City, State, Postal Code, Country/Region, Location Code, Attention, Address Owner Type, Local Tax Number, Parent Address.
At C:\ISS p2p GIL\httpwebrequest post addresses.ps1:10 char:1
+ Invoke-WebRequest `
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

However, in NAV when I use HTTPWebRequest, I only get the error message:
The remote server returned an error: (400) Bad Request.

I tried using Codeunit 1297 to return the webexception, and tried all the properties in websexception, but I could still not get the same detailed error I got in Powershell.

Best Answer

  • vaprogvaprog Member Posts: 1,144
    Answer ✓
    Try WebException.Response.GetResponseStream

Answers

  • vaprogvaprog Member Posts: 1,144
    Answer ✓
    Try WebException.Response.GetResponseStream
  • jordi79jordi79 Member Posts: 277
    Hi Vaprog.
    It works like charm.
    Thanks
Sign In or Register to comment.