1
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

However, if I am trying to delete like

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

it works

Why is not working in the first way using $postParams ?

GodEater
  • 3,445
  • 2
  • 27
  • 30
Star123
  • 669
  • 2
  • 8
  • 19
  • please see the correct command executed without missing the cutoff – Star123 Feb 20 '17 at 10:09
  • 1
    can we please not tag these with "curl" since these are not *real* curl commands? – Daniel Stenberg Feb 20 '17 at 11:11
  • So you can tell from the expection that you're using Powershell's alias "curl", which really calls the Invoke-Webrequest cmdlet. Is that what you're intending? Or are you trying to call a real curl binary? – GodEater Feb 20 '17 at 11:19

2 Answers2

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

This is working

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
Star123
  • 669
  • 2
  • 8
  • 19
0

EDIT

It's failing because DELETE isn't a POST command.

The code below is untested.

To recreate the DELETE in PowerShell, your syntax needs to be:

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

ORIGINAL POST

This relates to the commandline app curl, not the PowerShell curl which is an alis for Invoke-WebRequest

It's failing for two reasons, The first one is that DELETE isn't a POST command. The second, is that you're trying to pass a PowerShell object into a commandline application.

The code below is untested.

To recreate the DELETE in PowerShell, your syntax needs to be:

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

A POST command could be like this (depending on your endpoint):

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

Note, the body is a json string, not a PowerShell object.

TechSpud
  • 3,418
  • 1
  • 27
  • 35
  • 1
    In response to your second point: No he's not - he's passing it into the poorly chosen alias for Invoke-Webrequest, which should expect to deal with powershell objects. – GodEater Feb 20 '17 at 11:17
  • 1
    Good point, @GodEater. Forgot about the alias. WIll completely amend my post and strikethrough the bad parts! – TechSpud Feb 20 '17 at 11:20
  • No strikethrough in Markdown - have marked the edit & original – TechSpud Feb 20 '17 at 11:27
  • the post request is not working still i tried .. please see above answer – Star123 Feb 20 '17 at 12:35
  • @Star123 - a DELETE request isn't a POST request. See this answer for a morfe compelte explanation - http://stackoverflow.com/a/299696/1368849 – TechSpud Feb 20 '17 at 12:39
  • yeah thanks i got it . So DELETE I am trying it the one explained by you.But the second part i a trying to understand is now using POST i am trying to update a existing eventId which is not working – Star123 Feb 20 '17 at 12:42
  • I want to understand why even POST is nt working even though my goal is to update something using POST – Star123 Feb 20 '17 at 12:44
  • Try specifying the header, as in my original answer and also specifying the content type. Also, you're specifying DELETE with POST data. YOu'd need to send a POST request. – TechSpud Feb 20 '17 at 12:45