如何使用Invoke-WebRequest的GET方法构建查询字符串 [英] How to use GET method of Invoke-WebRequest to build a query string

查看:129
本文介绍了如何使用Invoke-WebRequest的GET方法构建查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在使用PowerShell中的Invoke-WebRequest或Invoke-RestMethod通过查询字符串从网页获取信息的标准方法?

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?

例如,我知道与格式正确的JSON端点一起使用时,以下内容将起作用:

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

以及等效的Invoke-WebMethod.其中一个重要方面是内容类型和ConvertTo-JSON,它可以将-Body部分中指定的参数转换为标准形式,包括"Children"字段的数组形式.

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.

使用这样的网站的等效方法是什么,该网站使用逗号分隔的约定来管理URL中的数组参数,或者使用诸如"Children [] = Abe& Children [] = Karen& 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"?

是否存在我所缺少的内容类型,是否存在等效的ConvertTo-?我可以使用?我的猜测是以前有人必须这样做.

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.

对于上下文,这是经常使用的 在URL中编码数组参数的一种方法,通常在PHP网站中看到.

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的引用(特定上下文除外),并调整了标题以引用查询字符串.问题是关于编码查询字符串而不是PHP本身.

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.

推荐答案

运行PHP的服务器似乎与此处无关.我想您是在问如何将键/值对作为查询字符串参数发送.

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.

如果是这样,那么您很幸运. Invoke-RestMethod

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

编辑

现在看到的问题是,您希望查询字符串参数具有多个值,本质上是一个数组,这排除了您可以传递给body参数的数据类型.

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.

因此,让我们首先从[UriBuilder]对象开始并添加使用[HttpValueCollection]对象(允许重复键)构建的查询字符串来逐步构建URI.

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

这篇关于如何使用Invoke-WebRequest的GET方法构建查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆