HTTP - Test URL

Octopus.Script exported 2020-06-16 by bobjwalker belongs to ‘HTTP’ category.

Makes a GET request to a HTTP(S) end point and verifies that a particular status code and (optional) response is returned within a specified period of time.

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

URI

Uri =

The full Uri of the endpoint

Custom HOST header

CustomHostHeader =

An optional custom HOST header which will be passed with the request

Security Protocol

SecurityProtocol =

The optional security protocol version to use for HTTPS requests.

Expected code

ExpectedCode = 200

The expected HTTP status code

Timeout (Seconds)

TimeoutSeconds = 60

The number of seconds before the step fails and times out

Username

AuthUsername =

Username for authentication. Leave blank to use Anonymous.

Password

AuthPassword =

Password for authentication. Leave blank for Anonymous.

Use Windows Authentication

UseWindowsAuth = False

Should the request be made passing windows authentication (kerberos) credentials otherwise uses basic authentication

Expected Response

ExpectedResponse =

The response should be this text

Script body

Steps based on this template will execute the following PowerShell script.

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$uri = $OctopusParameters['Uri']
$customHostHeader = $OctopusParameters['CustomHostHeader']
$expectedCode = [int] $OctopusParameters['ExpectedCode']
$timeoutSeconds = [int] $OctopusParameters['TimeoutSeconds']
$Username = $OctopusParameters['AuthUsername']
$Password = $OctopusParameters['AuthPassword']
$UseWindowsAuth = [System.Convert]::ToBoolean($OctopusParameters['UseWindowsAuth'])
$ExpectedResponse = $OctopusParameters['ExpectedResponse']
$securityProtocol = $OctopusParameters['SecurityProtocol']

Write-Host "Starting verification request to $uri"
if ($customHostHeader)
{
    Write-Host "Using custom host header $customHostHeader"
}

Write-Host "Expecting response code $expectedCode."
Write-Host "Expecting response: $ExpectedResponse."

if ($securityProtocol)
{
    Write-Host "Using security protocol $securityProtocol"
    [Net.ServicePointManager]::SecurityProtocol = [Enum]::parse([Net.SecurityProtocolType], $securityProtocol) 
}

$timer = [System.Diagnostics.Stopwatch]::StartNew()
$success = $false
do
{
    try
    {
        if ($Username -and $Password -and $UseWindowsAuth)
        {
            Write-Host "Making request to $uri using windows authentication for user $Username"
            $request = [system.Net.WebRequest]::Create($uri)
            $Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $(ConvertTo-SecureString -String $Password -AsPlainText -Force)
            $request.Credentials = $Credential 
            
            if ($customHostHeader)
            {
                $request.Host = $customHostHeader
            }

            try
            {
                $response = $request.GetResponse()
            }
            catch [System.Net.WebException]
            {
                Write-Host "Request failed :-( System.Net.WebException"
                Write-Host $_.Exception
                $response = $_.Exception.Response
            }
            
        }
		elseif ($Username -and $Password)
        {
            Write-Host "Making request to $uri using basic authentication for user $Username"
            $Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $(ConvertTo-SecureString -String $Password -AsPlainText -Force)
            if ($customHostHeader)
            {
                $response = Invoke-WebRequest -Uri $uri -Method Get -UseBasicParsing -Credential $Credential -Headers @{"Host" = $customHostHeader} -TimeoutSec $timeoutSeconds
            }
            else 
            {
                $response = Invoke-WebRequest -Uri $uri -Method Get -UseBasicParsing -Credential $Credential -TimeoutSec $timeoutSeconds
            }
        }
		else
        {
            Write-Host "Making request to $uri using anonymous authentication"
            if ($customHostHeader)
            {
                $response = Invoke-WebRequest -Uri $uri -Method Get -UseBasicParsing -Headers @{"Host" = $customHostHeader} -TimeoutSec $timeoutSeconds
            }
            else 
            {
                $response = Invoke-WebRequest -Uri $uri -Method Get -UseBasicParsing -TimeoutSec $timeoutSeconds
            }
        }
        
        $code = $response.StatusCode
        $body = $response.Content;
        Write-Host "Recieved response code: $code"
        Write-Host "Recieved response: $body"

        if($response.StatusCode -eq $expectedCode)
        {
            $success = $true
        }
        if ($success -and $ExpectedResponse)
        {
            $success = ($ExpectedResponse -eq $body)
        }
    }
    catch
    {
        # Anything other than a 200 will throw an exception so
        # we check the exception message which may contain the 
        # actual status code to verify
        
        Write-Host "Request failed :-("
        Write-Host $_.Exception

        if($_.Exception -like "*($expectedCode)*")
        {
            $success = $true
        }
    }

    if(!$success)
    {
        Write-Host "Trying again in 5 seconds..."
        Start-Sleep -s 5
    }
}
while(!$success -and $timer.Elapsed -le (New-TimeSpan -Seconds $timeoutSeconds))

$timer.Stop()

# Verify result

if(!$success)
{
    throw "Verification failed - giving up."
}

Write-Host "Sucesss! Found status code $expectedCode"

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "f5cebc0a-cc16-4876-9f72-bfbd513e6fdd",
  "Name": "HTTP - Test URL",
  "Description": "Makes a GET request to a HTTP(S) end point and verifies that a particular status code and (optional) response is returned within a specified period of time.",
  "Version": 18,
  "ExportedAt": "2020-06-16T08:03:12.574Z",
  "ActionType": "Octopus.Script",
  "Author": "bobjwalker",
  "Packages": [],
  "Parameters": [
    {
      "Id": "ca7c3e92-c243-4115-a326-7693eb830214",
      "Name": "Uri",
      "Label": "URI",
      "HelpText": "The full Uri of the endpoint",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "0d760ed5-81ed-46d9-b833-74f75df08bfc",
      "Name": "CustomHostHeader",
      "Label": "Custom HOST header",
      "HelpText": "An optional custom HOST header which will be passed with the request",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "b4e57984-01df-4438-96e5-75e74c2c6188",
      "Name": "SecurityProtocol",
      "Label": "Security Protocol",
      "HelpText": "The optional security protocol version to use for HTTPS requests.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "Select",
        "Octopus.SelectOptions": "SystemDefault\nSsl3\nTls\nTls11\nTls12"
      }
    },
    {
      "Id": "8f47469e-3b6d-4915-a710-3a601debeb8a",
      "Name": "ExpectedCode",
      "Label": "Expected code",
      "HelpText": "The expected HTTP status code",
      "DefaultValue": "200",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "7c96b2b3-53dd-4281-ae1a-ef67cbc0eb72",
      "Name": "TimeoutSeconds",
      "Label": "Timeout (Seconds)",
      "HelpText": "The number of seconds before the step fails and times out",
      "DefaultValue": "60",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "ac0b303c-0c59-4776-be21-d93ebe9e28e7",
      "Name": "AuthUsername",
      "Label": "Username",
      "HelpText": "Username for authentication. Leave blank to use Anonymous.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "38eae17a-3098-48df-b8dc-96c3185f9f40",
      "Name": "AuthPassword",
      "Label": "Password",
      "HelpText": "Password for authentication. Leave blank for Anonymous.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "Sensitive"
      }
    },
    {
      "Id": "60fe87fe-e96d-448e-94fa-f2ce4bfbaf3a",
      "Name": "UseWindowsAuth",
      "Label": "Use Windows Authentication",
      "HelpText": "Should the request be made passing windows authentication (kerberos) credentials otherwise uses basic authentication",
      "DefaultValue": "False",
      "DisplaySettings": {
        "Octopus.ControlType": "Checkbox"
      }
    },
    {
      "Id": "47c779b5-c515-49f9-a122-f692b0f12ff7",
      "Name": "ExpectedResponse",
      "Label": "Expected Response",
      "HelpText": "The response should be this text",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptBody": "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12\n\n$uri = $OctopusParameters['Uri']\n$customHostHeader = $OctopusParameters['CustomHostHeader']\n$expectedCode = [int] $OctopusParameters['ExpectedCode']\n$timeoutSeconds = [int] $OctopusParameters['TimeoutSeconds']\n$Username = $OctopusParameters['AuthUsername']\n$Password = $OctopusParameters['AuthPassword']\n$UseWindowsAuth = [System.Convert]::ToBoolean($OctopusParameters['UseWindowsAuth'])\n$ExpectedResponse = $OctopusParameters['ExpectedResponse']\n$securityProtocol = $OctopusParameters['SecurityProtocol']\n\nWrite-Host \"Starting verification request to $uri\"\nif ($customHostHeader)\n{\n    Write-Host \"Using custom host header $customHostHeader\"\n}\n\nWrite-Host \"Expecting response code $expectedCode.\"\nWrite-Host \"Expecting response: $ExpectedResponse.\"\n\nif ($securityProtocol)\n{\n    Write-Host \"Using security protocol $securityProtocol\"\n    [Net.ServicePointManager]::SecurityProtocol = [Enum]::parse([Net.SecurityProtocolType], $securityProtocol) \n}\n\n$timer = [System.Diagnostics.Stopwatch]::StartNew()\n$success = $false\ndo\n{\n    try\n    {\n        if ($Username -and $Password -and $UseWindowsAuth)\n        {\n            Write-Host \"Making request to $uri using windows authentication for user $Username\"\n            $request = [system.Net.WebRequest]::Create($uri)\n            $Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $(ConvertTo-SecureString -String $Password -AsPlainText -Force)\n            $request.Credentials = $Credential \n            \n            if ($customHostHeader)\n            {\n                $request.Host = $customHostHeader\n            }\n\n            try\n            {\n                $response = $request.GetResponse()\n            }\n            catch [System.Net.WebException]\n            {\n                Write-Host \"Request failed :-( System.Net.WebException\"\n                Write-Host $_.Exception\n                $response = $_.Exception.Response\n            }\n            \n        }\n\t\telseif ($Username -and $Password)\n        {\n            Write-Host \"Making request to $uri using basic authentication for user $Username\"\n            $Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $(ConvertTo-SecureString -String $Password -AsPlainText -Force)\n            if ($customHostHeader)\n            {\n                $response = Invoke-WebRequest -Uri $uri -Method Get -UseBasicParsing -Credential $Credential -Headers @{\"Host\" = $customHostHeader} -TimeoutSec $timeoutSeconds\n            }\n            else \n            {\n                $response = Invoke-WebRequest -Uri $uri -Method Get -UseBasicParsing -Credential $Credential -TimeoutSec $timeoutSeconds\n            }\n        }\n\t\telse\n        {\n            Write-Host \"Making request to $uri using anonymous authentication\"\n            if ($customHostHeader)\n            {\n                $response = Invoke-WebRequest -Uri $uri -Method Get -UseBasicParsing -Headers @{\"Host\" = $customHostHeader} -TimeoutSec $timeoutSeconds\n            }\n            else \n            {\n                $response = Invoke-WebRequest -Uri $uri -Method Get -UseBasicParsing -TimeoutSec $timeoutSeconds\n            }\n        }\n        \n        $code = $response.StatusCode\n        $body = $response.Content;\n        Write-Host \"Recieved response code: $code\"\n        Write-Host \"Recieved response: $body\"\n\n        if($response.StatusCode -eq $expectedCode)\n        {\n            $success = $true\n        }\n        if ($success -and $ExpectedResponse)\n        {\n            $success = ($ExpectedResponse -eq $body)\n        }\n    }\n    catch\n    {\n        # Anything other than a 200 will throw an exception so\n        # we check the exception message which may contain the \n        # actual status code to verify\n        \n        Write-Host \"Request failed :-(\"\n        Write-Host $_.Exception\n\n        if($_.Exception -like \"*($expectedCode)*\")\n        {\n            $success = $true\n        }\n    }\n\n    if(!$success)\n    {\n        Write-Host \"Trying again in 5 seconds...\"\n        Start-Sleep -s 5\n    }\n}\nwhile(!$success -and $timer.Elapsed -le (New-TimeSpan -Seconds $timeoutSeconds))\n\n$timer.Stop()\n\n# Verify result\n\nif(!$success)\n{\n    throw \"Verification failed - giving up.\"\n}\n\nWrite-Host \"Sucesss! Found status code $expectedCode\"",
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Script.ScriptSource": "Inline"
  },
  "Category": "HTTP",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/http-test-url.json",
  "Website": "/step-templates/f5cebc0a-cc16-4876-9f72-bfbd513e6fdd",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAIAAAAiOjnJAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAB1dJREFUeNrsnc8uO18Yh1ERoRKJ+CYkLNljzwVwAXoBdQHs6V7t6wLaC+AC2GPPykJIxM6fECm/T3qS8zumM9PTabUz+jyr0Z7OTOc8877vnDlTw9/f30MA3WYYsQCxALEAsTgKgFiAWIBYAIgFiAWIBYBYgFiAWACIBYgFiA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ugEEabgDEAkAsQCxALADEAsQCxAJALEAsQCwAxALEAsQCQCxALEAsAMQCxALEAkAsQCxALADEAsSCv0qSR+wBEAsQCxALALEAsQCxABALEAsQCwCxALEAsQAQCxALEAsAsQCxALEAEAsQCxALALEAsQCxABALEAsQCwCxALEAsQAQCxALMsV/AgwA3l1/9Yi7khIAAAAASUVORK5CYII=",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Tuesday, June 16, 2020