233

我正在尝试 POST 到 uri,并发送参数username=me

Invoke-WebRequest -Uri http://example.com/foobar -Method POST

如何使用 POST 方法传递参数?

4

4 回答 4

347

将您的参数放在哈希表中并像这样传递它们:

$postParams = @{username='me';moredata='qwerty'}
Invoke-WebRequest -Uri http://example.com/foobar -Method POST -Body $postParams
于 2013-06-26T21:40:00.617 回答
114

对于一些挑剔的 Web 服务,请求需要将内容类型设置为 JSON,并将正文设置为 JSON 字符串。例如:

Invoke-WebRequest -UseBasicParsing http://example.com/service -ContentType "application/json" -Method POST -Body "{ 'ItemID':3661515, 'Name':'test'}"

或 XML 等的等价物。

于 2016-05-19T12:43:38.577 回答
29

这很有效:

$body = @{
 "UserSessionId"="12345678"
 "OptionalEmail"="MyEmail@gmail.com"
} | ConvertTo-Json

$header = @{
 "Accept"="application/json"
 "connectapitoken"="97fe6ab5b1a640909551e36a071ce9ed"
 "Content-Type"="application/json"
} 

Invoke-RestMethod -Uri "http://MyServer/WSVistaWebClient/RESTService.svc/member/search" -Method 'Post' -Body $body -Headers $header | ConvertTo-HTML
于 2018-09-27T22:26:53.987 回答
13

用作 POST api 调用JSON的主体时没有 ps 变量的单个命令:{lastName:"doe"}

Invoke-WebRequest -Headers @{"Authorization" = "Bearer N-1234ulmMGhsDsCAEAzmo1tChSsq323sIkk4Zq9"} `
                  -Method POST `
                  -Body (@{"lastName"="doe";}|ConvertTo-Json) `
                  -Uri https://api.dummy.com/getUsers `
                  -ContentType application/json

查看更多:启动您的 PowerShell

于 2019-12-19T16:05:02.857 回答