format_url

This page explains how to use the format_url function in APL.

The format_url function constructs a properly formatted URL from a dynamic object containing URL components (scheme, host, path, port, etc.). Use this function when you need to build URLs programmatically from parsed components or when reconstructing URLs from log data.

Usage

Syntax

format_url(url_parts)

Parameters

NameTypeRequiredDescription
url_partsdynamicYesA dynamic object containing URL components: scheme, host, path, port, fragment, user, password, query.

Returns

Returns a properly formatted URL string constructed from the provided components.

Use case examples

Reconstruct full URLs from parsed components to analyze complete request patterns.

Query

['sample-http-logs']
| extend full_url = format_url(dynamic({'scheme': 'https', 'host': 'api.example.com', 'path': uri}))
| project _time, method, status, full_url
| limit 10

Run in Playground

Output

_timemethodstatusfull_url
2024-11-06T10:00:00ZGET200https://api.example.com/api/users
2024-11-06T10:01:00ZPOST201https://api.example.com/api/orders
2024-11-06T10:02:00ZGET200https://api.example.com/api/products

This query reconstructs full URLs from URI paths by adding the scheme and host, useful for generating clickable links in reports.

Build URLs from trace attributes to identify the full endpoints being called.

Query

['otel-demo-traces']
| extend service_url = format_url(dynamic({'scheme': 'http', 'host': 'localhost', 'port': 8080, 'path': strcat('/', ['service.name'])}))
| project _time, ['service.name'], service_url, trace_id
| limit 10

Run in Playground

Output

_timeservice.nameservice_urltrace_id
2024-11-06T10:00:00Zfrontendhttp://localhost:8080/frontendabc123
2024-11-06T10:01:00Zcheckouthttp://localhost:8080/checkoutdef456
2024-11-06T10:02:00Zcarthttp://localhost:8080/cartghi789

This query constructs service URLs from trace data, helping visualize the actual endpoints in a distributed system.

Construct URLs with authentication parameters to audit access attempts.

Query

['sample-http-logs']
| extend access_url = format_url(dynamic({'scheme': 'https', 'host': 'secure.example.com', 'path': uri, 'user': id}))
| project _time, access_url, status, ['geo.country']
| limit 10

Run in Playground

Output

_timeaccess_urlstatusgeo.country
2024-11-06T10:00:00Zhttps://user123@secure.example.com/admin403United States
2024-11-06T10:01:00Zhttps://user456@secure.example.com/api/secret401Unknown

This query constructs complete URLs including user information for failed authentication attempts, helping security teams understand the full context of access attempts.

  • parse_url: Parses a URL string into its components. Use this to reverse the formatting operation and extract URL parts.
  • parse_urlquery: Parses URL query parameters. Use this when you need to work with query string parameters specifically.
  • url_encode: Encodes a string for safe use in URLs. Use this to encode individual URL components before formatting.
  • strcat: Concatenates strings. Use this for simple URL construction without the structure of format_url.

Other query languages