Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use GET method of Invoke-WebRequest to build a query string

Is there a standard way to make use of Invoke-WebRequest or Invoke-RestMethod in PowerShell to get information from a web page using a query string?

For example, I know that the following when used with a well formed JSON end point will work:

$Parameters = @{
  Name = 'John'
  Children = 'Abe','Karen','Jo'
}
$Result = Invoke-WebRequest -Uri 'http://.....whatever' -Body ( $Parameters | ConvertTo-Json)  -ContentType application/json -Method Get

along with the equivalent Invoke-WebMethod. An important aspect of this is the content type and ConvertTo-JSON which manages the transformation of the parameters specified in the -Body part in to a standard form, including the array aspect of the "Children" field.

What is an equivalent way to do this with a website which uses, say, a comma delimited convention for managing array arguments in the URL or an approach such as "Children[]=Abe&Children[]=Karen&Children=Jo"?

Is there a content type that I'm missing and is there an equivalent ConvertTo-?? that I can use? My guess is that someone has had to do this before.

For context this is an often used way of encoding an array parameter in the URL and is commonly seen in PHP web sites.

passing arrays as url parameter

Edit Removed references to PHP except for specific context and adjusted the title to refer to a query string. The problem is about encoding a query string not PHP per se.

like image 535
user1383092 Avatar asked Sep 07 '15 16:09

user1383092


1 Answers

It seems that the server running PHP is irrelevant here. I think you're asking how to send key/value pairs as query string parameters.

Simple Case

If that's the case, you're in luck. Both Invoke-RestMethod and Invoke-WebRequest will take a [hashtable] in the body and construct your query string for you:

$Parameters = @{
    Name = 'John'
    Children = 'Abe','Karen','Jo'
}
Invoke-WebRequest -Uri 'http://www.example.com/somepage.php' -Body $Parameters -Method Get # <-- optional, Get is the default

Passing Complex Objects

Now seeing that the issue is that you want a query string parameter to have multiple values, essentially an array, this rules out the data types you can pass to the body parameter.

So instead, let's build the URI piece by piece first by starting with a [UriBuilder] object and adding on a query string built using an [HttpValueCollection] object (which allows duplicate keys).

$Parameters = [System.Web.HttpUtility]::ParseQueryString([String]::Empty)
$Parameters['Name'] = 'John'
foreach($Child in @('Abe','Karen','Joe')) {
    $Parameters.Add('Children', $Child)
}

$Request = [System.UriBuilder]'http://www.example.com/somepage.php'

$Request.Query = $Parameters.ToString()

Invoke-WebRequest -Uri $Request.Uri -Method Get # <-- optional, Get is the default
like image 71
briantist Avatar answered Oct 24 '22 13:10

briantist