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

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

For example, I know that the following when used with a well-formed JSON endpoint 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 controls the conversion of the parameters specified in the -Body part to a standard form, including the array field aspect of the Children field.

What is the equivalent way to do this using a website that uses, for example, a comma-delimited convention to control array arguments in a URL or an approach such as "Children [] = Abe & Children [] = Karen & Children = Joe "?

Is there a type of content that I am missing and is there an equivalent to ConvertTo- ?? what can i use? I guess someone should have done this before.

In context, this often uses a method of encoding an array parameter in a URL and is commonly seen on PHP websites.

passing arrays as a URL parameter

Edit Removed links to PHP except for a specific context and adjusted the header for links to the query string. The problem is encoding the query string, not PHP as such.

+4
2

, , PHP, . , , / .

, . Invoke-RestMethod Invoke-WebRequest [hashtable] :

$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

, , , , , , body.

URI, [UriBuilder] , [HttpValueCollection] ( ).

$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
+2

, " ". @briantist , .NET HttpValueCollection. , " " .

-, , -. , ( , -).

# Setup, parameters is now a PowerShell hash table
# we convert that on the fly to an appropriate URL
$Parameters = @{
    Name = 'John'
    Children = 'Abe','Karen','Jo'
}
$Uri = 'http://example.com/somepage.php'
$AddSquareBracketsToArrayParameters = $true



$HttpValueCollection = [System.Web.HttpUtility]::ParseQueryString([String]::Empty)
foreach ($Item in $Parameters.GetEnumerator()) {
    if ($Item.Value.Count -gt 1) {
        # It is an array, so treat that as a special case.
        foreach ($Value in $Item.Value) {
            # Add each item in the array, optionally mark the name of the parameter
            # to indicate it is an array parameter.
            $ParameterName = $Item.Key
            if ($AddSquareBracketsToArrayParameters) { $ParameterName += '[]' }                 
            $HttpValueCollection.Add($ParameterName, $Value)
        }
    } else {
        # Add the scalar value.
        $HttpValueCollection.Add($Item.Key,$Item.Value)
    }
}
# Build the request and load it with the query string.
$Request  = [System.UriBuilder]($Uri)
$Request.Query = $HttpValueCollection.ToString()

# Now fire off the request.
Invoke-WebRequest -Uri $Request.Uri
+1

Source: https://habr.com/ru/post/1606286/


All Articles