Skip to main content
urlencode(str) percent-encodes str so it is safe to use as a URL query parameter or path segment. Special characters like spaces, &, =, and / are encoded.

Signature

urlencode(str string) string

Example

Build a search query URL for a monitoring dashboard link in a release notification:
variable "app" {
  type    = string
  default = "api"
}

variable "version" {
  type = string
}

computed "dashboard_url" {
  description = "Grafana link filtered to this release"
  expression  = "https://grafana.example.com/d/overview?var-app=${urlencode(var.app)}&var-version=${urlencode(var.version)}&from=now-1h"
}

task "post-release" {
  description = "Post release info with dashboard link"
  commands = [
    "curl -X POST https://slack.example.com/webhook -d '{\"text\": \"Deployed ${var.app} ${var.version}: ${computed.dashboard_url}\"}'",
  ]
}

Notes

  • urlencode encodes the entire string, including characters that are valid in some URL parts (like /). It is intended for query parameter values and similar contexts where the entire value must be escaped.
  • Spaces are encoded as +, following the application/x-www-form-urlencoded format used by url.QueryEscape. If your target requires %20 for spaces, replace + with %20 after encoding.