Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I split/parse a URL string into an object?

I'm trying to split a URL into an object.

$url = "https://api.somedomain.com/v2/direct-access/producing-entities-details?entity_id=104194825&format=json&page=1

The desired result would be

PS C:\WINDOWS\system32> $url.api 
https://api.somedomain.com/v2/direct-access

PS C:\WINDOWS\system32> $url.dataset
producing-entities-details

PS C:\WINDOWS\system32> $url.params
entity_id=104194825&format=json&page=1 

I'm sure this can be done in combination with regex, but I am also wondering if this can be done just using built in PowerShell functionality.

like image 512
Paula Erickson Avatar asked Dec 11 '22 04:12

Paula Erickson


1 Answers

No need to mess around with regex. The [uri] type accelerator would do some of that work for you. The other parts seem specific to how you choose to interpret the data and not how URL anatomy works.

PS C:\Users\matt> $url = [uri]"https://api.somedomain.com/v2/direct-access/producing-entities-details?entity_id=104194825&format=json&page=1"

PS C:\Users\matt> $url.Query
?entity_id=104194825&format=json&page=1

You can explore the other properties and see how they will help you. For instance, you might need to build the Segments to get the other parts you are looking for.

PS C:\Users\matt>   $url.Segments
/
v2/
direct-access/
producing-entities-details
like image 155
Matt Avatar answered Dec 20 '22 07:12

Matt