Star123
Star123

Reputation: 679

Unable to process http delete request using powershell curl command

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 ?

Upvotes: 1

Views: 8968

Answers (2)

Star123
Star123

Reputation: 679

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

Upvotes: 1

TechSpud
TechSpud

Reputation: 3518

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.

Upvotes: 0

Related Questions