无法使用powershell curl命令处理http删除请求

时间:2017-02-20 09:51:35

标签: powershell

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

但是,如果我想删除

$postParams

它有效

为什么不使用{{1}}第一种方式工作?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

修改

它失败了,因为DELETE不是POST命令。

以下代码未经测试。

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

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

原始帖子

这与命令行应用程序curl有关,而不是与curl

的alis的PowerShell Invoke-WebRequest有关

它失败有两个原因,第一个是DELETE不是POST命令。第二,是您尝试将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对象。