278

I am trying to send a DELETE request with a url parameter using CURL. I am doing:

curl -H application/x-www-form-urlencoded -X DELETE http://localhost:5000/locations` -d 'id=3'

However, the server is not seeing the parameter id = 3. I tried using some GUI application and when I pass the url as: http://localhost:5000/locations?id=3, it works. I really would rather use CURL rather than this GUI application. Can anyone please point out what I'm doing wrong?

darksky
  • 20,411
  • 61
  • 165
  • 254

2 Answers2

400

The application/x-www-form-urlencoded Content-type header is not required (well, kinda depends). Unless the request handler expects parameters coming from the form body. Try it out:

curl -X DELETE "http://localhost:5000/locations?id=3"

or

curl -X GET "http://localhost:5000/locations?id=3"
felipsmartins
  • 13,269
  • 4
  • 48
  • 56
  • 58
    It worked. I just realised the URL has to be between quotes to accept parameters. That MIME type is for a URL with parameters and thats what the GUI application uses. Also, I do not want to do `GET`. I want to `DELETE` and not `GET` and I am trying to follow proper REST design standards so I am using DELETE and not GET when deleting. – darksky Nov 14 '12 at 01:23
  • 5
    In my case it works only with double quotation, with single quotation in says curl: (1) Protocol 'http not supported or disabled in libcurl But with "" quotation works just fine. – Jeff_Alieffson Dec 07 '14 at 03:52
  • I am actually on Windows environment. Using single quotes didn't work for me. It thrown an error like " 'http" not supported. However it worked when I included them within double quotes. – Nilucshan Siva Oct 02 '20 at 15:44
238

@Felipsmartins is correct.

It is worth mentioning that it is because you cannot really use the -d/--data option if this is not a POST request. But this is still possible if you use the -G option.

Which means you can do this:

curl -X DELETE -G 'http://localhost:5000/locations' -d 'id=3'

Here it is a bit silly but when you are on the command line and you have a lot of parameters, it is a lot tidier.

I am saying this because cURL commands are usually quite long, so it is worth making it on more than one line escaping the line breaks.

curl -X DELETE -G \
'http://localhost:5000/locations' \
-d id=3 \
-d name=Mario \
-d surname=Bros

This is obviously a lot more comfortable if you use zsh. I mean when you need to re-edit the previous command because zsh lets you go line by line. (just saying)

starball
  • 20,030
  • 7
  • 43
  • 238
Mig
  • 2,381
  • 1
  • 11
  • 2
  • 15
    Life saver! Thanks man! I have a script where I want to use --data-urlencode on a GET. This made it so I don't have to manually url-encode my parameters. Thanks! – Nathan Wallace Sep 13 '13 at 15:31
  • 5
    Are you implying that -X DELETE -G is really a POST request? – huggie Feb 19 '14 at 14:36
  • 1
    ` -G, --get Put the post data in the URL and use GET` No, it just adds the post data to the url, -X [method] takes precedence (source: curl --help and experience) – DownloadPizza Aug 06 '19 at 10:54
  • 2
    How can this be so convoluted. One could assume `--data-urlencode` adds the data to the URL with no exceptions but now you need to combine it with `--get` to make it actually work. – Jaakko Nov 15 '19 at 09:18
  • zsh isn't really necessary, try the `fc` command - it puts the previous command into your editor, when you save and quit, it runs the command. – rreagan3 Oct 14 '22 at 04:13