November 21, 2024

Parsing URLs is super hard, but Power Automate has many functions that deal with this so nicely that it is a waste of time for us to even think about manually parsing URLs.

If you’re wondering why it’s not called urlHost? Or, more broadly, what is a URI? Auth0 has a good article explaining this, but the short version is that a URI is a global identifier that can identify a resource, while an URL is only a pointer to a location. So all URLs are URI, but not all URI are URLs. Here’s another article that can help. For the sake of this article, we’ll consider it similar to an URL (and used interchangeably) so as not to create confusion.

Now that we have that out of the way, let’s move to where we find 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

Let’s start with a simple example:

String 'https://manueltgomes.com/'

will return 

manueltgomes.com

If the value is in a variable, here’s how to use it.

uriHost(variables('URL'))
The variable contains: 'https://manueltgomes.com/'

will return 

manueltgomes.com

The function will get the domain and any other part. If you’re not familiar with the components, don’t worry. What you need to know is that the first part of the URI is the “host”, meaning that “www.google.com/maps”, for example, has the host as “www.google.com”.

In other words, the “uriHost” function gets a URI and removes the first “HTTP” part and everything after the “.com” or any different top-level domain like “.io” or “.coffee” (it’s a real one, believe me). It will keep the “www” part if you include it.

Let’s look at a longer URL:

https://manueltgomes.com/area/microsoft/powerautomate/

Will return:

manueltgomes.com

As mentioned before, everything after the top-level domain will be ignored by the “uriHost” function because it’s not part of the host.

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 that is even bigger than 1000, I would strongly advise that you break it into smaller, manageable formulas.

Another limitation is that the URI needs to be complete, meaning that you need to include the “HTTP” part; otherwise, you’ll get the following error:

Unable to process template language expressions in action 'Compose' inputs at line '0' and column '0': 'The template language function 'uriHost' expects its parameter to be a well-formed absolute URI. The provided value was 'www.google.com/maps'. Please see https://aka.ms/logicexpressions#uriHost for usage details.'.

In the above case, I provided the following expression:

uriHost('www.google.com/maps')

Although this makes sense and should not be considered a limitation per se, I would because Microsoft could default it to “HTTP” to make things easier instead of returning an error.

Recommendations:

Here are some things to keep in mind.

Don’t nest

There’s no real reason to do it, but if you find yourself in a situation where you have nested uriHost functions in a formula, you should review it and make everything more straightforward. You end up in the same place since the website that you’re providing already has only the host part. If this happens, either remove all the “uriHost” functions or check if one should not be there.

Always add a comment

Adding a comment will also help avoid mistakes. Indicate why you are getting the domain and how you’ll use it. It may look obvious initially but will not be in a few months or years. It’s essential to enable faster debugging when something goes wrong.

Sources:

Microsoft’s uriHost Function Reference

Back to the Power Automate Function Reference.

Photo by Kotagauni Srinivas 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