{"action":"created","issue":{"url":"https://api.github.com/repos/PowerShell/PowerShell/issues/5555","repository_url":"https://api.github.com/repos/PowerShell/PowerShell","labels_url":"https://api.github.com/repos/PowerShell/PowerShell/issues/5555/labels{/name}","comments_url":"https://api.github.com/repos/PowerShell/PowerShell/issues/5555/comments","events_url":"https://api.github.com/repos/PowerShell/PowerShell/issues/5555/events","html_url":"https://github.com/PowerShell/PowerShell/issues/5555","id":277051968,"number":5555,"title":"Original Response Body Unattainable from Web Cmdltes on Non-Success Status Codes","user":{"login":"markekraus","id":6509955,"avatar_url":"https://avatars3.githubusercontent.com/u/6509955?v=4","gravatar_id":"","url":"https://api.github.com/users/markekraus","html_url":"https://github.com/markekraus","followers_url":"https://api.github.com/users/markekraus/followers","following_url":"https://api.github.com/users/markekraus/following{/other_user}","gists_url":"https://api.github.com/users/markekraus/gists{/gist_id}","starred_url":"https://api.github.com/users/markekraus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/markekraus/subscriptions","organizations_url":"https://api.github.com/users/markekraus/orgs","repos_url":"https://api.github.com/users/markekraus/repos","events_url":"https://api.github.com/users/markekraus/events{/privacy}","received_events_url":"https://api.github.com/users/markekraus/received_events","type":"User","site_admin":false},"labels":[{"id":552752310,"url":"https://api.github.com/repos/PowerShell/PowerShell/labels/Area-Cmdlets-Utility","name":"Area-Cmdlets-Utility","color":"5319e7","default":false},{"id":311043050,"url":"https://api.github.com/repos/PowerShell/PowerShell/labels/Issue-Enhancement","name":"Issue-Enhancement","color":"fc2929","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2017-11-27T15:12:01Z","updated_at":"2017-11-29T06:57:47Z","closed_at":null,"author_association":"COLLABORATOR","body":"When a non-success status code is returned the web cmdlets provide a convenient  error message and attaches the `HttpResponseMessage` object to the `Response` property of the exception. In 5.1 cmdltes which wrap `Invoke-RestMethod` and `Invoke-WebRequest` could run the following to retrieve the raw response body:\r\n\r\n```powershell\r\ntry {\r\n    Invoke-WebRequest 'http://httpbin.org/status/418' -ErrorAction Stop\r\n}\r\nCatch {\r\n    $err = $_\r\n}\r\n$Stream = $err.Exception.Response.GetResponseStream()\r\n$Stream.Position = 0\r\n$Reader = [System.IO.StreamReader]::new($Stream)\r\n$Reader.ReadToEnd()\r\n```\r\n\r\nThis is needed because sometimes the response body for error codes contain useful information about why something failed. \r\n\r\nIn 6.0.0 the equivalent code is below in the \"Steps to Reproduce\", This would work except that the web cmdltes are disposing the stream after creating the error message:\r\n\r\nhttps://github.com/PowerShell/PowerShell/blob/32286ed22a0c8f3b58716d5d0fb2948c558678ec/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebRequestPSCmdlet.CoreClr.cs#L581\r\n\r\nThis makes it impossible to get the original stream. This would probably not be a problem except that there are encoding issues to consider and that the string in the error message is doctored to make it more human readable:\r\n\r\nhttps://github.com/PowerShell/PowerShell/blob/32286ed22a0c8f3b58716d5d0fb2948c558678ec/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebRequestPSCmdlet.CoreClr.cs#L571\r\n\r\nI recommend not disposing the streamreader and let it naturally fall out of scope as there does not appear to be a way to dispose of a streamreader without closing the underlying stream.\r\n\r\nSteps to reproduce\r\n------------------\r\n\r\n```powershell\r\ntry {\r\n    Invoke-WebRequest 'http://httpbin.org/status/418' -ErrorAction Stop\r\n}\r\nCatch {\r\n    $err = $_\r\n}\r\n$Stream = $err.Exception.Response.Content.ReadAsStreamAsync().GetAwaiter().GetResult()\r\n$Stream.Position = 0\r\n$Reader = [System.IO.StreamReader]::new($Stream)\r\n$Reader.ReadToEnd()\r\n```\r\n\r\nExpected behavior\r\n-----------------\r\n\r\n```none\r\n    -=[ teapot ]=-\r\n\r\n       _...._\r\n     .'  _ _ `.\r\n    | .\"` ^ `\". _,\r\n    \\_;`\"---\"`|//\r\n      |       ;/\r\n      \\_     _/\r\n        `\"\"\"`\r\n```\r\n\r\nActual behavior\r\n---------------\r\n\r\n```none\r\nException calling \".ctor\" with \"1\" argument(s): \"Stream was not readable.\"\r\nAt line:1 char:1\r\n+ $Reader = [System.IO.StreamReader]::new($Stream)\r\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException\r\n+ FullyQualifiedErrorId : ArgumentException\r\nYou cannot call a method on a null-valued expression.\r\nAt line:1 char:1\r\n+ $Reader.ReadToEnd()\r\n+ ~~~~~~~~~~~~~~~~~~~\r\n+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException\r\n+ FullyQualifiedErrorId : InvokeMethodOnNull\r\n```\r\n\r\nEnvironment data\r\n----------------\r\n\r\n```none\r\nName                           Value\r\n----                           -----\r\nPSVersion                      6.0.0-rc\r\nPSEdition                      Core\r\nGitCommitId                    v6.0.0-rc\r\nOS                             Microsoft Windows 10.0.15063\r\nPlatform                       Win32NT\r\nPSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}\r\nPSRemotingProtocolVersion      2.3\r\nSerializationVersion           1.1.0.1\r\nWSManStackVersion              3.0\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/PowerShell/PowerShell/issues/comments/347770394","html_url":"https://github.com/PowerShell/PowerShell/issues/5555#issuecomment-347770394","issue_url":"https://api.github.com/repos/PowerShell/PowerShell/issues/5555","id":347770394,"user":{"login":"dragonwolf83","id":20375201,"avatar_url":"https://avatars0.githubusercontent.com/u/20375201?v=4","gravatar_id":"","url":"https://api.github.com/users/dragonwolf83","html_url":"https://github.com/dragonwolf83","followers_url":"https://api.github.com/users/dragonwolf83/followers","following_url":"https://api.github.com/users/dragonwolf83/following{/other_user}","gists_url":"https://api.github.com/users/dragonwolf83/gists{/gist_id}","starred_url":"https://api.github.com/users/dragonwolf83/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dragonwolf83/subscriptions","organizations_url":"https://api.github.com/users/dragonwolf83/orgs","repos_url":"https://api.github.com/users/dragonwolf83/repos","events_url":"https://api.github.com/users/dragonwolf83/events{/privacy}","received_events_url":"https://api.github.com/users/dragonwolf83/received_events","type":"User","site_admin":false},"created_at":"2017-11-29T06:57:47Z","updated_at":"2017-11-29T06:57:47Z","author_association":"NONE","body":"Any reason why this has to be a Stream and not just parsed automatically into `Exception.RawMessage` or maybe `Exception.Response.RawMessage`? Memory usage? Maybe do both if backwards compatibility is the only concern? \r\n\r\nWhy is it stripping out the formatting? \r\n\r\nI hate the idea of having to do extra code to read the stream if the common scenario is to want that detail. "}}
{"actor":{"display_login":"dragonwolf83"}}