279

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?

2 Answers 2

401

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"
3
  • 59
    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, 2012 at 1: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. Dec 7, 2014 at 3: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. Oct 2, 2020 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)

5
  • 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! Sep 13, 2013 at 15:31
  • 5
    Are you implying that -X DELETE -G is really a POST request?
    – huggie
    Feb 19, 2014 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) Aug 6, 2019 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, 2019 at 9: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, 2022 at 4:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.