0

我在使用 Powershell 将字符串列表传递给 API 方法时遇到了一些困难。

我尝试了什么:我没有尝试什么?我的最新版本是这样的:

$uri = "http://localhost:15207/v1/API/GetFoos"

Add-Type -AssemblyName System.Core
$contents = New-Object System.Collections.Generic.List[string]

$contents.Add('Foo_1')
$contents.Add('Foo_1000002')

$body = @{
    "someParam"="string1"
    "contents"= $contents
}

Invoke-RestMethod -uri $uri -Body $body -Method Post

这会导致 API 接收到以下值:

[0] = "Foo_1 Foo_1000002"

但我尝试过数组,以及括号、方括号、单引号和双引号的各种不同组合。在所有情况下,contents 参数要么接收一个对象类型,要么接收一个值,它是我试图传递的值的串联组合。

我确信我不是第一个必须将字符串列表传递给 API 方法的人,但是搜索它包括许多导致不相关匹配的常用术语。

4

1 回答 1

1

而不是 Invoke-RestMethod,而是看一下 Invoke-WebRequest。也许这样的事情会起作用:

$uri = "http://localhost:15207/v1/API/GetFoos"

$body = @{}
$body.someParam = 'string1'
$body.contents = ('Foo1', 'Foo_1000002')
$requestJson = $body | ConvertTo_Json

$response = Invoke-WebRequest -uri $uri -Body $requestJson -ContentType "application/json" -Method Post

#Need to convert the JSON content into something we can use
$responseObject = $response.Content | ConvertFrom-Json
于 2014-08-07T21:01:48.887 回答