Introduction:
While socket programming with Python, I realized that I was getting some strange HTTP Status codes, but I had no idea what they actually meant.
I googled it and found instant results from a couple of cool websites, but I was wondering if can web harvest HTTP Status code information from a website and wrap it in a function which can query these statuses to add more verbosity to my web requests.
On top of that, once I have the HTTP Status codes and their description in a CSV, JSON or XML format, I can repurpose this data in any program/script as desired which is obviously way better than Googling it each and every time when troubleshooting such errors.
Making it work:
-
- First get information for all the HTTP status code, but where to get it? I figured out many websites that have such information like https://httpstatuses.com/ and if you inspect the source code of the website you can easily web harvest this data
Similarly, more detailed description of these HTTP status codes can be accessed by suffixing the URL with the status code like in the following image
So I wrote a small PowerShell script (below) to web request and capture this information from the website and export the result to CSV file.
- Once you have the information in CSV format you require this file to be accessible so that it can be easily downloaded over the internet from anywhere in the world.
In order to achieve this, I copy-pasted/committed it to my Github repository from where it could be downloaded to any machine with access to the internet since the file is Public. - Write a Powershell function that can
- Web request above mentioned public Github gist URL and downloads the contents of the CSV file.
- Filter out the HTTP status code you’re trying to search from the content and display its description
Following is the PowerShell function to get above done
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters<# .SYNOPSIS HTTP Status code information .DESCRIPTION Get detailed information of a HTTP Status code with type, short description and a long description. Which can add verbosity to your script results/outputs and making it easy to understand. .PARAMETER StatusCode HTTP Status code to lookup .EXAMPLE PS> Get-HTTPStatusCode -StatusCode 404, 303 StatusCode Type ShortDescription LongDescription ---------- ---- ---------------- --------------- 404 Client Error Not Found The origin server did not find a current representation for he target resource or is not willing to disclose that one the target resource or is not willing to disclose that one exists. rce, as indicated by a URI in the Location header field, 303 Redirection See Other The server is redirecting the user agent to a different inal request. resource, as indicated by a URI in the Location header field, which is intended to provide an indirect response to the original request. .EXAMPLE PS> 301, 404, 202 | Get-HTTPStatusCode StatusCode Type ShortDescription LongDescription ---------- ---- ---------------- --------------- 301 Redirection Moved Permanently The target resource has been assigned a new permanent URI and any future references to this resource ought to use one of the enclosed URIs. 404 Client Error Not Found The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. 202 Success Accepted The request has been accepted for processing, but the processing has not been completed. The request might or might not eventually be acted upon, as it might be disallowed when processing actually takes place. .NOTES Author : Prateek Singh Blog : http:\\ridicurious.com\PowerShell #> Function Get-HTTPStatusCode { Param( [Parameter( Mandatory = $true, HelpMessage = 'HTTP Status Code to lookup', ValueFromPipeline = $true, Position = 0 )] [ValidateNotNullOrEmpty()] [string[]] $StatusCode ) Begin{ $TempFileName = "$env:TEMP\HTTPStatusCode.csv" If(Test-Path $TempFileName){ $Data = Import-Csv $TempFileName } else{ $Data = Invoke-WebRequest 'https://raw.githubusercontent.com/PrateekKumarSingh/PowershellScrapy/master/HTTPStatusCodes/HTTPStatusCodes.csv' | ` ForEach-Object content | ` Tee-Object -FilePath $TempFileName -Verbose | ConvertFrom-Csv } } Process { Foreach($Item in $StatusCode){ ($Output = $Data.where({$Item -eq $_.StatusCode.trim()})) If(-not $Output) { Write-Error "Couldn't find HTTP status code information for $Item" } } } End{ Remove-Variable -Name Data; [gc]::Collect() } }
- When the Powershell function runs for the first time, it creates a local copy of content in a File at a static location, which could be accessed to get HTTP status codes information locally for all Future function calls by avoiding the Web request which is slower.
Took care of point-4 in my PowerShell function like in the screenshot below
- First get information for all the HTTP status code, but where to get it? I figured out many websites that have such information like https://httpstatuses.com/ and if you inspect the source code of the website you can easily web harvest this data
Running the Function:
You can run the function on demand like in the below screenshot.
NOTE – The first call to the function would be slow, but from the next call you have a local copy of the CSV file on your machine, which can be queried comparatively much faster to web requests.
Hope you’ll find the script useful, please don’t forget to share this script with your friends and colleagues.
Thanks for reading, and happy Deepawali to you and your Family! 🙂
Follow @singhprateik
[…] Query HTTP Status Code detailed information using Powershell […]
LikeLike
[…] Query HTTP Status Code detailed information using Powershell […]
LikeLike
[…] Query HTTP Status Code detailed information using Powershell […]
LikeLike