So you have a URL and want to get parts of it. One way would be to use the split function or indexOf function and parse it yourself, but there’s a better way by using one of the “uri” functions available in Power Automate like the “uriHost”, “uriPathAndQuery” and “uriQuery” functions to name a few. Today we’ll explore the “uriPath” function and, as the name indicates, get the URL’s path or the information after (with caveats) the top-level domain like “.com”.
Let’s see how to use it.
Where to find it?
You can find the function in every action where a formula is supported. For example, let’s look at a “Compose” action:
As you can see, we can auto-complete by using the “tab” key. Let’s look at how to use it.
It follows a simple pattern.
- String with the URL
Let’s start with a simple example:
Let’s try with a big URL.
String 'https://manueltgomes.com/area/microsoft/powerautomate/' will return /area/microsoft/powerautomate/
If the domain doesn’t have any path, it will not return an empty string, but it will return “/“. For example:
String 'https://manueltgomes.com' will return /
The “/“ is the default value for the path, indicating that the path is empty. This is one of the things that your browsers and systems do automatically that you don’t need to worry about. Still, it’s essential to know that this is the value to expect when there’s no path available in the URL, so if you need to validate if the path is empty, don’t validate against an empty string or null, for example.
How about an URL with parameters (also known as “query strings”)?
String 'https://manueltgomes.com?query=test' will return /
It returns an empty path and if you think about it makes sense. The “query string” is not part of the path, so returning it doesn’t make sense. If you want the “query string” as well, you have the “uriPathAndQuery” and “uriQuery” functions that can help you. This function should not be considered “everything after the top-level domain”, so please consider this.
Finally, what about a string that is not a valid URL?
String 'this is not a valid URL' will return Unable to process template language expressions in action 'Parse_the_URL' inputs at line '0' and column '0': 'The template language function 'uriPath' expects its parameter to be a well-formed absolute URI. The provided value was 'this is not a valid URL'. Please see https://aka.ms/logicexpressions#uriPath for usage details.'.
Notice that we need a valid URL to be provided to the function, so be sure you validate the string before running the function; otherwise, your Flow will fail.
Depending on the size of your string, your expression may return an error, even if it’s correct. Please note that the expressions have a max size of 8,192 characters. If you have an expression even bigger than 1000, I would strongly advise you to break it into smaller manageable formulas.
Here are some things to keep in mind.
If you want to parse an URL don’t do it manually
Always use the “uri” functions that Power Automate provides you. They do a lot of work and consider edge cases that you may not consider. Also, it’s much easier to use one function that does all the work for us than doing all the work ourselves.
Use “debug” compose actions
I recommend using Compose actions to have the values that go “in” the function. This way, if the value doesn’t make sense, you can understand, based on the parameters, why it was calculated that way. It’s useful, especially if you change something in the URL before parsing it, so you can see the value before the function parses it.
There’s no real reason to do it, but if you find yourself in a situation where you have nested “uriPath” functions in a formula, you should review it and make everything more straightforward. If you nest it, the Flow will fail since the result of the function is not a valid URL, so be sure not to do it.
Always add a comment
Adding a comment will also help avoid mistakes. Indicate why you are trying to find the element and what it means. It may look obvious initially but it will not be in a few months or years. It’s essential to enable faster debugging when something goes wrong.
Microsoft’s uriPath Function Reference
Back to the Power Automate Function Reference.
Photo by Matt Duncan on Unsplash