【发布时间】:2017-03-16 20:51:20
【问题描述】:
我有一个接受 x-www-form-urlencoded 的现有 REST API。 API需要参数apikey,在Postman中测试成功,如下图。
但是我需要使用 Powershell 调用这个 API。下面是我的代码:
$params = @{"apikey"="abcd1234"}
Invoke-WebRequest -Uri http://localhost:3030/api/v1/usergroupsync -Method POST -Body $params
#also tried below, no avail.
Invoke-RestMethod -Uri http://localhost:3030/api/v1/usergroupsync -Method POST -Body $params
但是我遇到了这个错误:
Invoke-WebRequest : The underlying connection was closed: An unexpected error occured on a receive At line:14 char:1
+ Invoke-WebRequest -Uri http://localhost:3030/api/v1/usergroupsync -Method POST -...
+==============================================================================
+ CategoryInfo : InvalidOperations: (System.Net.HttpWebRequest:HTTTpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebcmdletWebResponseException,Microsoft.Powershell.Commands.InvokeWebRequest
如果我删除 -Body,则没有错误,并且响应如预期的那样“API 密钥无效”,这意味着我的 REST API 验证正确。
所以我怀疑我的问题出在身体上的原因?关于如何解决这个问题的任何想法?
PS 版本为 4.0.0
PS C:\> $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
4 0 -1 -1
【问题讨论】:
标签: rest powershell