json 如何使用 Invoke-WebRequest 的 GET 方法构建查询字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32442777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use GET method of Invoke-WebRequest to build a query string
提问by user1383092
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?
是否有一种标准方法可以在 PowerShell 中使用 Invoke-WebRequest 或 Invoke-RestMethod 来使用查询字符串从网页中获取信息?
For example, I know that the following when used with a well formed JSON end point will work:
例如,我知道以下内容与格式良好的 JSON 端点一起使用时会起作用:
$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.
以及等效的 Invoke-WebMethod。其中一个重要方面是内容类型和 ConvertTo-JSON,它管理将 -Body 部分中指定的参数转换为标准形式,包括“Children”字段的数组方面。
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"?
对于使用逗号分隔约定来管理 URL 中的数组参数或诸如“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.
是否有我缺少的内容类型,是否有等效的 ConvertTo-?? 我可以用吗?我的猜测是之前有人不得不这样做。
For context this is an often used way of encoding an array parameter in the URL and is commonly seen in PHP web sites.
对于上下文,这是在 URL 中对数组参数进行编码的常用方法,并且在 PHP 网站中很常见。
passing arrays as url parameter
EditRemoved 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.
编辑删除了对 PHP 的引用,除了特定的上下文,并调整了标题以引用查询字符串。问题在于编码查询字符串而不是 PHP 本身。
回答by briantist
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.
似乎运行 PHP 的服务器在这里无关紧要。我想您是在问如何将键/值对作为查询字符串参数发送。
If that's the case, you're in luck. Both Invoke-RestMethodand Invoke-WebRequestwill take a [hashtable]in the body and construct your query string for you:
如果是这种情况,那么您很幸运。双方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
Edit
编辑
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.
现在看到问题是您希望查询字符串参数具有多个值,本质上是一个数组,这排除了您可以传递给 body 参数的数据类型。
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).
因此,让我们首先通过从一个[UriBuilder]对象开始并添加使用[HttpValueCollection]对象(允许重复键)构建的查询字符串来逐个构建 URI 。
$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
回答by user1383092
The following appears to work reasonably well as a "first cut". Thanks to @briantist for the key point which was to use the .NET HttpValueCollection. It seems we have to "roll our own" way of building the query string.
以下似乎作为“第一次剪辑”工作得相当好。感谢@briantist 提供了使用 .NET HttpValueCollection 的关键点。似乎我们必须“推出我们自己的”方式来构建查询字符串。
The below code snip shows a simple way to transform the hash table containing parameters and values in to a well formed query string by simply traversing the hash table. One limitation is that nesting is not permitted (a parameter value cannot be a complex type such as a hash table).
下面的代码片段显示了一种简单的方法,通过简单地遍历哈希表,将包含参数和值的哈希表转换为格式良好的查询字符串。一个限制是不允许嵌套(参数值不能是复杂类型,例如哈希表)。
# 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

