June 24, 2024

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”, “uriPath” and “uriQuery” functions to name a few. Today we’ll explore the “uriPathAndQuery” function and, as the name indicates, get the URL’s path and the query string (also known as parameters) 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.

Usage

It follows a simple pattern.

  1. 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

/?query=test

Until now, it behaved as the “uriPath” function, but now we’re also getting the parameters (or query string) for the URL since it’s the whole point of the “uriPathAndQuery” function.

Let’s look at an example with a path and a query string.

String 'https://manueltgomes.com/area/microsoft/powerautomate/?query=test'

will return

/area/microsoft/powerautomate/?query=test

Although it’s tempting to look at it, this function does not return everything after the top-level domain blindly. There are edge cases to consider, like a URL that contains a port, for example. These cases are rare, but still, the function is not the same as cutting the domain part of the URL.

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 'uriPathAndQuery' 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#uriPathAndQuery 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.

Limitations

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.

Recommendations:

Here are some things to keep in mind.

If you want only the path, don’t use this function

There’s a function, the “uriPath” function, that returns only the path in the URL, so don’t try to use this function and remove the query string if it exists. The “uriPath” function can help you and does all the parsing correctly.

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, using one function that does all the work for us is much easier 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.

Don’t nest

There’s no real reason to do it, but if you find yourself in a situation where you have nested “uriPathAndQuery” 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.

Sources:

Microsoft’s uriPathAndQuery Function Reference

Back to the Power Automate Function Reference.

Photo by Carlos Arthur M.R on Unsplash

 

Manuel Gomes

I have 18 years of experience in automation, project management, and development. In addition to that, I have been writing for this website for over 3 years now, providing readers with valuable insights and information. I hope my expertise allows me to create compelling, informative content that resonates with the audience.

View all posts by Manuel Gomes →

Leave a Reply

Your email address will not be published. Required fields are marked *