304

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

418

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"
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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.
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.
259

@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 Comments

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!
Are you implying that -X DELETE -G is really a POST request?
` -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)
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.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.