HTTP - Post Form

Octopus.Script exported 2015-11-13 by kburdett belongs to ‘HTTP’ category.

Execute a simple form POST via HTTP. The script will construct a body in “application/x-www-form-urlencoded” format by extracting Octopus variables with a specified prefix (from the “HTTP.PostForm.Prefix” parameter). Variable names and values are encoded accordingly.

For instance, if “HTTP.PostForm.Prefix” is “foo.”, then the Octopus variable “foo.Bar”=“baz” will be translated to “Bar=baz” in the request body.

Parameters

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

URI

HTTP.PostForm.URI

Specifies the destination of the POST.

Prefix

HTTP.PostForm.Prefix = HTTP.Parameter.

Specifies the variable prefix for composing the body of the POST. Any variables in the current context that begin with this prefix will be extracted and added to the body.

Script body

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

try {
    $uri = $OctopusParameters["HTTP.PostForm.URI"]
    $prefix = $OctopusParameters["HTTP.PostForm.Prefix"]
    $body = ""

    # Ensure URI is populated
    if ([string]::IsNullOrEmpty($uri)) {
        Write-Error "HTTP.PostForm.URI is required and cannot be empty"
        return
    }

    # Construct the body
    if (![string]::IsNullOrEmpty($prefix)) {
        $params = $OctopusParameters.Keys |
            Where-Object { $_.StartsWith($prefix) } |
            Foreach-Object {
                $key = [uri]::EscapeDataString($_.Substring($prefix.Length))
                $val = [uri]::EscapeDataString($OctopusParameters[$_])
                Write-Verbose "Found parameter ${key}=${val}"
                "${key}=${val}"
            }
    
        if ($params.Length -gt 0) {
            $body = $params -join "&"
        }
    }
    
    # Execute the request
    Write-Host "Executing trigger: ${uri}"
    Write-Host "Body: ${body}"
    Invoke-WebRequest -Method Post -Uri $uri -Body $body -ContentType "application/x-www-form-urlencoded"
} catch {
    $ErrorMessage = $_.Exception.Message
    Write-Error $ErrorMessage
}

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": "3c7a59bd-d66e-4dc6-9509-895b4a5d98c8",
  "Name": "HTTP - Post Form",
  "Description": "Execute a simple form POST via HTTP. The script will construct a body in \"application/x-www-form-urlencoded\" format by extracting Octopus variables with a specified prefix (from the \"HTTP.PostForm.Prefix\" parameter). Variable names and values are encoded accordingly.\n\nFor instance, if \"HTTP.PostForm.Prefix\" is \"foo.\", then the Octopus variable \"foo.Bar\"=\"baz\" will be translated to \"Bar=baz\" in the request body.",
  "Version": 2,
  "ExportedAt": "2015-11-13T21:57:26.839+00:00",
  "ActionType": "Octopus.Script",
  "Author": "kburdett",
  "Parameters": [
    {
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      },
      "HelpText": "Specifies the destination of the POST.",
      "Label": "URI",
      "Name": "HTTP.PostForm.URI"
    },
    {
      "DefaultValue": "HTTP.Parameter.",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      },
      "HelpText": "Specifies the variable prefix for composing the body of the POST. Any variables in the current context that begin with this prefix will be extracted and added to the body.",
      "Label": "Prefix",
      "Name": "HTTP.PostForm.Prefix"
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptBody": "try {\r    $uri = $OctopusParameters[\"HTTP.PostForm.URI\"]\r    $prefix = $OctopusParameters[\"HTTP.PostForm.Prefix\"]\r    $body = \"\"\r\r    # Ensure URI is populated\r    if ([string]::IsNullOrEmpty($uri)) {\r        Write-Error \"HTTP.PostForm.URI is required and cannot be empty\"\r        return\r    }\r\r    # Construct the body\r    if (![string]::IsNullOrEmpty($prefix)) {\r        $params = $OctopusParameters.Keys |\r            Where-Object { $_.StartsWith($prefix) } |\r            Foreach-Object {\r                $key = [uri]::EscapeDataString($_.Substring($prefix.Length))\r                $val = [uri]::EscapeDataString($OctopusParameters[$_])\r                Write-Verbose \"Found parameter ${key}=${val}\"\r                \"${key}=${val}\"\r            }\r    \r        if ($params.Length -gt 0) {\r            $body = $params -join \"&\"\r        }\r    }\r    \r    # Execute the request\r    Write-Host \"Executing trigger: ${uri}\"\r    Write-Host \"Body: ${body}\"\r    Invoke-WebRequest -Method Post -Uri $uri -Body $body -ContentType \"application/x-www-form-urlencoded\"\r} catch {\r    $ErrorMessage = $_.Exception.Message\r    Write-Error $ErrorMessage\r}",
    "Octopus.Action.Script.Syntax": "PowerShell"
  },
  "Category": "HTTP",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/http-post-form.json",
  "Website": "/step-templates/3c7a59bd-d66e-4dc6-9509-895b4a5d98c8",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAIAAAAiOjnJAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAB1dJREFUeNrsnc8uO18Yh1ERoRKJ+CYkLNljzwVwAXoBdQHs6V7t6wLaC+AC2GPPykJIxM6fECm/T3qS8zumM9PTabUz+jyr0Z7OTOc8877vnDlTw9/f30MA3WYYsQCxALEAsTgKgFiAWIBYAIgFiAWIBYBYgFiAWACIBYgFiAWAWIBYgFgAiAWIBYgFgFiAWIBYAIgFiAWIBYBYgFiAWACIBYgFiNXM19fX+/s7x25AGB8fHxkZafdTowm2JBfr9TpHfEBIltNGOHDwGyAWIBYgFiAWAGIBYgFiASAWIBYgFgBiAWIBYgEgFiAWIBYAYgFiAWIBIBYgFiAWAGIBYgFiASAWIBYgFkByRnu/ybe3N457hhgeHs7n8+kV6+vr6+npSVZNT0/ncjk6LCt8fHwkECvJD6/V6/V2o46suru7m52d/ffvH12VLWSIglZKxXp8fFSgmpmZoZ8GRKxeFO+fn58Kp1jFVWH3xVK44lgjFgBiAWIBYgEgFiAWIBYAYgFiAWIBIBYgFiAWAGJBahjNyo5eXl7a5aWlpampKTpPHBwc3N/fa2GrQWibarV6dnamhfn5ebVHrB/s7OzY5Uqlsrq62vk6n5+fb25uWq5KvbKxsZHCY6L9Pz09NcuFQiGqmdroaxqxSIW/js5jneJuIAwNk2qjlun8CiYOGaJOD3PyxLcZ6IjV3axaLpft4Y7qD7Ux8WBubi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7P44E+8vWMQXwS2p1ooEBSKBT0ihaKDczrgc4rlUom6ugaPmZulgRS725vb5s7PNfX17VazZ1hIS30bic1XAqfTUWsyFRo8pQ7ZLDUwA1mpk1UeDNqDjUm6hwdHYW2UUEdNdunZchM87OppML/pdnf3w+tUQIprPk/i6qDQ4t0NY6f+aR3lXaT3RJu99nUvhdYWYpYZip6aJ4KYOetx6DyWc3U03beUrNDZkZypVJxB4e0ZsWk5h3QK8qVMkAfCUQ1KaVs27KzA7vtziBt99nUNIjVi/+wqsb6SL/mb/we7tx59ye77AN9cqUrg0k+02C0RRNKu7VRS7L/sDqINdZvYyYZd3GFPmtL2+RHBkgBsQCxALEAEAsQCwYZhhuSs7W1Ze8OpfaXlfsFA6TQgmQDpKRCoMYCxALEAkAsQCxALIBsilWv1znWiNVlxsfHYx7mBMRKuo2RkcnJydvbWw734NCLWzqGx8dHbWtxcXFsbIzjniGS3dLpnVji9fVVOfH9/R23MsTExMTCwkKqxYKMipXL5RhugEEabgDEAkAsQCxALADEAsQCxAJALEAsQCwAxALEAsQCQCxALEAsAMQCxALEAkAsQCxALADEAsSCv0qSR+wBEAsQCxALALEAsQCxABALEAsQCwCxALEAsQAQCxALEAsAsQCxALEAEAsQCxALALEAsQCxABALEAsQCwCxALEAsQAQCxALMsV/AgwA3l1/9Yi7khIAAAAASUVORK5CYII=",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Friday, November 13, 2015