Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoseGonzalez321/6b2cca09d91892f2490daad814f50994 to your computer and use it in GitHub Desktop.
Save JoseGonzalez321/6b2cca09d91892f2490daad814f50994 to your computer and use it in GitHub Desktop.
This script checks that a website is responding with a 200 using FSharp
// From
// https://fsharpforfunandprofit.com/posts/low-risk-ways-to-use-fsharp-at-work-2/#dev-website-responding
//This script checks that a website is responding with a 200.
//This might be useful as the basis for a post-deployment smoke test
#r @"..\Packages\FSharp.Data\lib\net40\FSharp.Data.dll"
open FSharp.Data
let queryServer uri queryParams =
try
let response = Http.Request(uri, query=queryParams, silentHttpErrors = true)
Some response
with
| :? System.Net.WebException as ex -> None // | :? Matching on subtypes - crude polymorphism
let sendAlert uri message =
printfn "Error for %s. Message=%O" uri message
// match...with explanation https://fsharpforfunandprofit.com/posts/match-expression/
// match is the new switch/case...except that matter is important!
let checkServer (uri, queryParams) =
match queryServer uri queryParams with
| Some response ->
printfn "Response for %s is %O" uri response.StatusCode
if (response.StatusCode <> 200) then
sendAlert uri response.StatusCode
| None ->
sendAlert uri "No response"
let google = "http://google.com", ["q", "fsharp"]
let bad = "http://example.bad", []
[google; bad]
|> List.iter checkServer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment