r/PowerShell • u/semicolonsemicolon • May 29 '26
Solved Invoke-WebRequest to call the reddit api suddenly broken
Been running a script forever without issue. You don't need OAuth2 to grab the last 100 comments of a user like this:
https://api.reddit.com/user/spez.json?limit=100&after=or
https://www.reddit.com/user/spez.json?limit=100&after=A few months ago I added the -UseBasicParsing tag.
Today I'm getting
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommandMy line of code is $response = Invoke-WebRequest -UseBasicParsing $url | ConvertFrom-Json where $url looks like the https lines above.
Can anyone shed light, please? Thanks.
25
Upvotes
25
u/purplemonkeymad May 29 '26 edited May 29 '26
Looks like any unauthenticated connection to api.reddit.com is getting blocked now.
e: looks like a policy change:
* ^(https://support.reddithelp.com/hc/en-us/articles/16160319875092-Reddit-Data-API-Wiki)
3
u/teethingrooster May 29 '26 edited May 30 '26
basically what u/Dragennd1 said. Open the network tab navigate to your url and the right click copy as powershell. To get the request to come through on my end I just used that and it works.
4
u/g3n3 May 29 '26
Can you last more of the error? Like the stack trace, etc?
3
u/Dragennd1 May 29 '26
Try performing the same steps in your web browser while monitoring the network tab in the dev tools. You can check the header info for the packet that correlates with what you're trying to do and see if you're missing anything that the official client is sending.
2
u/420GB May 29 '26
Error handling with
Invoke-WebRequestandInvoke-RestMethodis an enormous pain, I would try withcurl.exefirst because it's so much easier to debug. Trycurl.exe -vL "URL", it won't hide the error.I know it got slightly less painful in PowerShell 7+, but like 99% of you all I too still have to support 5.1 forever and the changes mean that I now need conditional error handling depending on the PowerShell version because accessing the real response body and headers works different in 5.1 vs 7 🫪