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

""

The URL will return an empty string if it doesn’t have any query string.

If you’re used other “uri” functions you know that this behaviour is a bit different than “uriPath” or “uriPathAndQuery” functions for example. These functions will return “/” if they cannot find information instead of an empty string

Let’s try with parameters and see what we get:

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

will return

?query=test

Notice that if you used the “uriPathAndQuery” function above, you would get something slightly different:

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

will return

/?query=test

This is because every function that deals with the path in some way, like “uriPath” or “uriPathAndQuery” consider the “empty” path as “/“ and not as an empty string, so the “/“ is always included.

Let’s try an URL with a path:

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

will return 

?query=test

As expected, the path would be ignored, and we only get the query string.

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 'uriQuery' 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#uriQuery 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 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 “uriQuery” 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 uriQuery Function Reference

Back to the Power Automate Function Reference.

Photo by Towfiqu barbhuiya 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 *

Mastodon