Post AchL10lN3d4fO2mrke by jaykul@fosstodon.org
(DIR) More posts by jaykul@fosstodon.org
(DIR) Post #Acb1Hvy6yMqlPMmLoG by emaksovalec@emacs.ch
2023-12-08T10:01:21Z
0 likes, 0 repeats
#powershell vs. unix scripting: calling a REST API endpoint and wanting to see full response even in case of 400-500 response type.linux: curl -i http://localhost/api/foopowershell: try { Invoke-WebRequest -UseBasicParsing "http://localhost/api/foo" } catch [System.Net.WebException] {$webExObj = $_.Exception; $webExObj.Response; [System.IO.StreamReader]::new($webExObj.Response.GetResponseStream()).ReadToEnd()}would love to use some better approach for PS but that is all I got currently
(DIR) Post #AcbORWjPxKUwyjRufw by falkheiland@fosstodon.org
2023-12-08T14:20:48Z
0 likes, 0 repeats
@emaksovalec `irm http://localhost/api/foo`would be the posh equivalent.irm is an alias for the cmdlet Invoke-RestMethod
(DIR) Post #AcbSrZ6a9YBjgaxJaK by emaksovalec@emacs.ch
2023-12-08T15:10:20Z
0 likes, 0 repeats
@falkheiland yes, but on non success status code (400-500) it throws an error and you don't get to see content. That is why I have that try catch block in my OP
(DIR) Post #Acbb3jLUhiQkYSF5xQ by falkheiland@fosstodon.org
2023-12-08T16:42:10Z
0 likes, 0 repeats
@emaksovalec oh yes sorry, completely skipped that status code part.i am going with a method similar to this https://github.com/MicrosoftDocs/PowerShell-Docs/issues/4456#issuecomment-503236939 in my usecases.
(DIR) Post #AcbnTaspzJ1jHxwC9Y by rmbolger@mastodon.social
2023-12-08T19:01:10Z
0 likes, 0 repeats
@emaksovalec Requires #powershell 7.x (which is cross platform, btw).(iwr http://localhost/api/foo -SkipHt).RawContent`iwr` is an alias for Invoke-WebRequest. `-SkipHt` is short for -SkipHttpErrorCheck. `RawContent` gets you the raw response body including headers as a string. Switch to `Content` to exclude the headers. Or `Headers` for just the headers collection object.
(DIR) Post #AchL10lN3d4fO2mrke by jaykul@fosstodon.org
2023-12-09T06:35:24Z
0 likes, 0 repeats
@rmbolger @emaksovalec Yes! If you use Invoke-RestMethod -SkipHttpError then you get the json (as an object) back.In current versions, the error message has the full content in it, so you can see it directly in the (error) output when you just call it without any extra parameters.irm -Method Post http://localhost:8000To capture it, just use the bash-like || to handle the error case and get the .Messageirm -Method Post http://localhost:8000/api/example -ev Fail || $Fail.Message
(DIR) Post #AchL11gnc70mG9ejGS by emaksovalec@emacs.ch
2023-12-11T11:10:39Z
0 likes, 0 repeats
@jaykul @rmbolger thanks for the answer, but over here we are sadly still on PS version 5 so I don't get the goodies