DEV Community

Barret Blake
Barret Blake

Posted on • Originally published at barretblake.dev on

Function Friday – URI Parsing Functions

Sometimes you need to work with URI (uniform resource identifiers) in Power Automate. URIs point at something, like websites, files, and so forth. You should be familiar with their most common format, the URL (uniform resource locator):

https://barretblake.dev/blog/?hello=23
Enter fullscreen mode Exit fullscreen mode

The first portion (“https”) identifies the protocol or scheme. The second portion (“barretblake.dev”) points to the domain, or server, where the target is located. And the next portion (“blog”) points to the specific resource on that target server. A URI can also include optional parameters to be passed in. In the example above we’re passing in the parameter “hello” and a value for it of “23”.

There are quite a few different schemes, including the ability for apps to define custom schemes that will allow those apps to respond to URIs themselves.

In Power Automate, there are times when you need to pick apart the various pieces of a URI for use within a flow. Thankfully, the system provides a number of functions to make picking apart a URI and getting its values quite simple.

uriScheme

The uriScheme function will pull out the scheme, or protocol, from the URI. The pattern is simple:

uriScheme('<URI>')
Enter fullscreen mode Exit fullscreen mode

In all of these functions, the URI parameter is a string value and can be passed in hard coded, as a variable, or as the output of a previous action or trigger.

Examples:

uriScheme('https://barretblake.dev') // returns 'https'
uriScheme('telnet://192.168.0.1') // returns 'telnet'
uriScheme('file:///c:/temp/test.txt') // returns 'file'
Enter fullscreen mode Exit fullscreen mode

uriHost

The uriHost function returns the host value from the URI. In most cases, this will be the server, or domain name, of the target.

uriHost('<URI>')
Enter fullscreen mode Exit fullscreen mode

Examples:

uriHost('https://barretblake.dev') // returns 'barretblake.dev'
uriHost('telnet://192.168.0.1') // returns '192.168.0.1'
uriHost('file:///c:/temp/test.txt') // returns 'c:'
Enter fullscreen mode Exit fullscreen mode

uriPort

For URIs where the port to be connected to is not the default port number, then it is added to the host section of the URI. The uriPort function will pull out that value. In the cases where the URI passed in does not have a port, the function will return the default port for that scheme, if there is one.

uriPort('<URI>')
Enter fullscreen mode Exit fullscreen mode

Examples:

uriPort('https://localhost:8080/index.html') // returns '8080'
uriPort('https://barretblake.dev') // returns '443'
Enter fullscreen mode Exit fullscreen mode

uriPath

The uriPath function returns the path segment, which is everything after the host and port and before the query portion. This is the segment that points at the actual resource to be accessed.

uriPath('<URI>')
Enter fullscreen mode Exit fullscreen mode

Examples:

uriPath('https://barretblake.dev/blog') // returns '/blog'
uriPath('file:///c:/temp/test.txt') // returns '/temp/text.txt')
Enter fullscreen mode Exit fullscreen mode

uriQuery

This function will return anything that’s part of the query, if there is one (i.e. anything after the “?” indicator).

uriQuery('<URI>')
Enter fullscreen mode Exit fullscreen mode

Example:

uriQuery('https://barretblake.dev/blog?name=barret') // returns '?name=barret'
Enter fullscreen mode Exit fullscreen mode

uriPathAndQuery

While uriPath and uriQuery return just the path or query segments respectively, the uriPathAndQuery function returns both together.

uriPathAndQuery('<URI>')
Enter fullscreen mode Exit fullscreen mode

Example:

uriPathAndQuery('https://barretblake.dev/blog?name=barret') // returns '/blog?name=barret'
Enter fullscreen mode Exit fullscreen mode

Conclusion

There are a lot of situations where you need to pick out one or more pieces of a URI. This block of functions will help you do just that.

We’ve almost reached the end. Next week, for our final Function Friday, we’ll delve into the manipulation functions which will let us alter and work with JSON and XML data.

The post Function Friday – URI Parsing Functions first appeared on Barret Codes.

Top comments (0)