无法使用PowerShell Curl命令处理HTTP删除请求


PS C:> $postParams = @{eventId='235'}
PS C:> curl -Method DELETE -Uri http://localhost:8080/eventlist/api/v1/events -Body $postParams
curl : Error deleting event
At line:1 char:1
+ curl -Method DELETE -Uri http://localhost:8080/eventlist/api/v1/events -Body $po ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], Web
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

但是,如果我想删除

curl -Method DELETE -Uri http://localhost:8080/eventlist/api/v1/events?eventId=235

它有效

为什么不使用$postParams

以第一种方式工作
This is not working
    PS C:Users> $postParams = "{eventId='$eventId'}"
    PS C:Users> Invoke-WebRequest -Method POST -Uri "http://localhost:8080/eventlist/api/v1/events" -Body $postParams
Invoke-WebRequest : Error creating event
At line:1 char:1
+ Invoke-WebRequest -Method POST -Uri "http://localhost:8080/eventlist/api/v1/even ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

这在工作

PS C:> Invoke-WebRequest -Method DELETE -Uri 'http://localhost:8080/eventlist/api/v1/events?eventId=235'

StatusCode        : 200
StatusDescription : OK
Content           : Event deleted successfully
RawContent        : HTTP/1.1 200 OK
                    Content-Length: 26
                    Content-Type: text/plain;charset=ISO-8859-1
                    Date: Mon, 20 Feb 2017 12:27:46 GMT
                    Server: Apache-Coyote/1.1
                    Event deleted successfully
Forms             : {}
Headers           : {[Content-Length, 26], [Content-Type, text/plain;charset=ISO-8859-1], [Date, Mon, 20 Feb 2017
                    12:27:46 GMT], [Server, Apache-Coyote/1.1]}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 26

edit

这是失败的,因为删除不是邮政命令。

未经测试的代码。

要在PowerShell中重新创建DELETE,您的语法必须为:

$eventId=235
Invoke-WebRequest -Method DELETE -Uri "http://localhost:8080/eventlist/api/v1/events?eventId=$eventId"

原始帖子

这与命令行应用程序卷曲有关,而不是powershell curl,它是Invoke-WebRequest

的ALIS

失败的原因有两个,第一个是删除不是邮政命令。第二,您正在尝试将PowerShell对象传递到命令行应用程序中。

未经测试的代码。

要在PowerShell中重新创建DELETE,您的语法必须为:

$eventId=235
&curl -Method DELETE -Uri "http://localhost:8080/eventlist/api/v1/events?eventId=$eventId"

POST命令可以像这样(取决于您的端点):

$eventId=235
$postParams = "{eventId='$eventId'}"
&curl -H "Content-Type: application/json" -X POST -d $postParams 'http://localhost:8080/eventlist/api/v1/events'

注意,身体是JSON字符串,而不是PowerShell对象。

最新更新