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 “uriScheme” function and, as the name indicates, get the URL’s schema or something like “https” for example.
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 an URL:
String 'https://manueltgomes.com/' will return https
As expected, we’ll get the schema of the URL or the “https” part of the URL, but what if the URL doesn’t contain it?
String 'www.manueltgomes.com' will return Unable to process template language expressions in action 'Parse_the_URL' inputs at line '0' and column '0': 'The template language function 'uriScheme' expects its parameter to be a well-formed absolute URI. The provided value was 'www.manueltgomes.com'. Please see https://aka.ms/logicexpressions#uriScheme for usage details.'.
The keyword here is “absolute URI”, meaning that we need to include the “http” part of it; otherwise, the “uriScheme” function won’t have anything to parse.
Let’s take a look at URLs with more information:
String 'https://manueltgomes.com/area/microsoft/powerautomate/?query=test' will return https
Please don’t assume that all URLs will return “https” or “http” since there are a lot of potential URL schemas. For example:
String 'ftp://ExampleURLftp:firstname.lastname@example.org/ExampleFolder/ExampleSubFolder' will return ftp
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 'uriScheme' 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#uriScheme 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, 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.
There’s no real reason to do it, but if you find yourself in a situation where you have nested “uriScheme” 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.
Back to the Power Automate Function Reference.