Skip to content

Commit 4320aaa

Browse files
committed
Add new URI functions
1 parent ff93eba commit 4320aaa

File tree

2 files changed

+170
-1
lines changed

2 files changed

+170
-1
lines changed

articles/azure-resource-manager/bicep/bicep-functions-string.md

Lines changed: 168 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Bicep functions - string
33
description: Describes the functions to use in a Bicep file to work with strings.
44
ms.topic: reference
55
ms.custom: devx-track-bicep
6-
ms.date: 02/14/2025
6+
ms.date: 05/09/2025
77
---
88

99
# String functions for Bicep
@@ -139,6 +139,92 @@ The output from the preceding example with the default values is:
139139
| toStringOutput | String | one, two, three |
140140
| toJsonOutput | Object | {"one": "a", "two": "b"} |
141141

142+
## buildUri
143+
144+
`buildUri(scheme, host, path, query, fragment)`
145+
146+
Constructs a URI by combining the provided scheme, host, path, query, and fragment components into a single URI string. To parse a URI, see [parseUri](#parseuri).
147+
148+
Namespace: [sys](bicep-functions.md#namespaces-for-functions).
149+
150+
### Parameters
151+
152+
| Parameter | Required | Type | Description |
153+
|:--- |:--- |:--- |:--- |
154+
| scheme | Yes | string | The protocol of the URI (e.g., `http`, `https`, `ftp`). |
155+
| host | Yes | string | The domain or hostname (e.g., `example.com`). |
156+
| path | Yes | string | The path component (e.g., `/path/to/resource`). |
157+
| query | No | string | The query string, including the leading `?` if present (e.g., `?key=value`). Defaults to an empty string if not provided. |
158+
| fragment | No | string | The fragment identifier, including the leading `#` if present (e.g., `#section`). Defaults to an empty string if not provided. |
159+
160+
* The `scheme`, `host`, and `path` parameters are required to ensure a valid URI structure.
161+
* The `query` and `fragment` parameters are optional and can be omitted or set to an empty string (`''`) if not needed.
162+
* The function handles proper formatting, ensuring correct separators (e.g., `://` between scheme and host, `/` for paths, `?` for queries, `#` for fragments) and avoids issues like double slashes.
163+
* For complete details, the URI is constructed as specified in [RFC 3986, section 3](https://tools.ietf.org/html/rfc3986#section-3).
164+
165+
### Return Value
166+
167+
A string representing the absolute URI constructed from the provided components.
168+
169+
### Examples
170+
171+
The following example shows how to use `buildUri` to construct a URI from individual components:
172+
173+
```bicep
174+
var scheme = 'https'
175+
var host = 'mystorage.blob.core.windows.net'
176+
var path = '/templates/nestedTemplate.json'
177+
var query = '?st=2025-05-09'
178+
var fragment = '#section'
179+
180+
var constructedUri = buildUri(scheme, host, path, query, fragment)
181+
182+
output uriOutput string = constructedUri
183+
```
184+
185+
The output from the preceding example is:
186+
187+
| Name | Type | Value |
188+
| ---- | ---- | ----- |
189+
| uriOutput | String | `https://mystorage.blob.core.windows.net/templates/nestedTemplate.json?st=2025-05-09#section` |
190+
191+
### Additional Example: Combining with `parseUri`
192+
193+
The following example shows how to use `parseUri` to extract components from an existing URI, modify them, and then use `buildUri` to reconstruct a new URI:
194+
195+
```bicep
196+
param originalUri string = 'https://mystorage.blob.core.windows.net/data/file.json?st=2025-05-09'
197+
198+
var parsedUri = parseUri(originalUri)
199+
var newPath = '/newdata/newfile.json'
200+
var newQuery = '?newParam=value'
201+
202+
var newUri = buildUri(parsedUri.scheme, parsedUri.host, newPath, newQuery, parsedUri.fragment)
203+
204+
output originalHost string = parsedUri.host
205+
output newUri string = newUri
206+
```
207+
208+
The output from the preceding example is:
209+
210+
| Name | Type | Value |
211+
| ---- | ---- | ----- |
212+
| originalHost | String | `mystorage.blob.core.windows.net` |
213+
| newUri | String | `https://mystorage.blob.core.windows.net/newdata/newfile.json?newParam=value` |
214+
215+
```bicep
216+
param originalUri string = 'https://mystorage.blob.core.windows.net/data/file.json?st=2025-05-09'
217+
218+
var parsedUri = parseUri(originalUri)
219+
var newPath = '/newdata/newfile.json'
220+
var newQuery = '?newParam=value'
221+
222+
var newUri = buildUri(parsedUri.scheme, parsedUri.host, newPath, newQuery, parsedUri.fragment)
223+
224+
output originalHost string = parsedUri.host
225+
output newUri string = newUri
226+
```
227+
142228
## concat
143229

144230
`concat(arg1, arg2, arg3, ...)`
@@ -894,6 +980,87 @@ The output from the preceding example with the default values is:
894980
| ---- | ---- | ----- |
895981
| stringOutput | String | 0000000123 |
896982

983+
## parseUri
984+
985+
`parseUri(uriString)`
986+
987+
Parses a URI string into its constituent components, such as scheme, host, path, query, and fragment. To build a URI string, see [buildUri](#builduri).
988+
989+
Namespace: [sys](bicep-functions.md#namespaces-for-functions).
990+
991+
### Parameters
992+
993+
| Parameter | Required | Type | Description |
994+
|:--- |:--- |:--- |:--- |
995+
| uriString | Yes | string | The URI string to parse. Must be a valid URI as per RFC 3986. |
996+
997+
### Return Value
998+
999+
An object containing the parsed URI components with the following properties:
1000+
1001+
| Property | Type | Description |
1002+
|:--- |:--- |:--- |
1003+
| scheme | string | The protocol of the URI (e.g., `http`, `https`, `ftp`). |
1004+
| host | string | The domain or hostname (e.g., `example.com`). |
1005+
| port | int or null | The port number (e.g., `443`), or `null` if not specified. |
1006+
| path | string | The path component (e.g., `/path/to/resource`). |
1007+
| query | string | The query string, including the leading `?` (e.g., `?key=value`), or empty string if not present. |
1008+
| fragment | string | The fragment identifier, including the leading `#` (e.g., `#section`), or empty string if not present. |
1009+
| authority | string | The full authority component (e.g., `example.com:443`), combining host and port if present. |
1010+
| isAbsolute | bool | Indicates whether the URI is absolute (i.e., includes a scheme). |
1011+
1012+
For complete details, the URI is parsed as specified in [RFC 3986, section 3](https://tools.ietf.org/html/rfc3986#section-3).
1013+
1014+
### Examples
1015+
1016+
The following example shows how to use `parseUri` to extract components from a URI:
1017+
1018+
```bicep
1019+
param inputUri string = 'https://mystorage.blob.core.windows.net/templates/nestedTemplate.json?st=2025-05-09#section'
1020+
1021+
var parsedUri = parseUri(inputUri)
1022+
1023+
output scheme string = parsedUri.scheme
1024+
output host string = parsedUri.host
1025+
output path string = parsedUri.path
1026+
output query string = parsedUri.query
1027+
output fragment string = parsedUri.fragment
1028+
output isAbsolute bool = parsedUri.isAbsolute
1029+
```
1030+
1031+
The output from the preceding example with the default values is:
1032+
1033+
| Name | Type | Value |
1034+
| ---- | ---- | ----- |
1035+
| scheme | String | `https` |
1036+
| host | String | `mystorage.blob.core.windows.net` |
1037+
| path | String | `/templates/nestedTemplate.json` |
1038+
| query | String | `?st=2025-05-09` |
1039+
| fragment | String | `#section` |
1040+
| isAbsolute | Bool | `true` |
1041+
1042+
### Additional Example: Combining with `uri`
1043+
1044+
The following example shows how to use `parseUri` to extract the host and scheme, then reconstruct a new URI with a different path using the `uri` function:
1045+
1046+
```bicep
1047+
param originalUri string = 'https://mystorage.blob.core.windows.net/data/file.json?st=2025-05-09'
1048+
1049+
var parsedUri = parseUri(originalUri)
1050+
var newPath = '/newdata/newfile.json'
1051+
var modifiedUri = uri('${parsedUri.scheme}://${parsedUri.host}', newPath)
1052+
1053+
output originalHost string = parsedUri.host
1054+
output newUri string = modifiedUri
1055+
```
1056+
1057+
The output from the preceding example is:
1058+
1059+
| Name | Type | Value |
1060+
| ---- | ---- | ----- |
1061+
| originalHost | String | `mystorage.blob.core.windows.net` |
1062+
| newUri | String | `https://mystorage.blob.core.windows.net/newdata/newfile.json` |
1063+
8971064
## replace
8981065

8991066
`replace(originalString, oldString, newString)`

articles/azure-resource-manager/bicep/bicep-functions.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ Bicep provides the following functions for working with strings. All of these fu
174174
* [base64](./bicep-functions-string.md#base64)
175175
* [base64ToJson](./bicep-functions-string.md#base64tojson)
176176
* [base64ToString](./bicep-functions-string.md#base64tostring)
177+
* [buildUri](./bicep-functions-string.md#builduri)
177178
* [concat](./bicep-functions-string.md#concat)
178179
* [contains](./bicep-functions-string.md#contains)
179180
* [dataUri](./bicep-functions-string.md#datauri)
@@ -190,6 +191,7 @@ Bicep provides the following functions for working with strings. All of these fu
190191
* [length](./bicep-functions-string.md#length)
191192
* [newGuid](./bicep-functions-string.md#newguid)
192193
* [padLeft](./bicep-functions-string.md#padleft)
194+
* [parseUri](./bicep-functions-string.md#parseuri)
193195
* [replace](./bicep-functions-string.md#replace)
194196
* [skip](./bicep-functions-string.md#skip)
195197
* [split](./bicep-functions-string.md#split)

0 commit comments

Comments
 (0)