CURL Command Line URL Parameters
Asked Answered
M

2

286

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?

Metallo answered 14/11, 2012 at 0:30 Comment(0)
C
409

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"
Cycad answered 14/11, 2012 at 0:41 Comment(3)
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.Metallo
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.Bibulous
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.Trash
D
242

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

Desulphurize answered 11/1, 2013 at 18:34 Comment(5)
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!Coben
Are you implying that -X DELETE -G is really a POST request?Sjoberg
` -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)Libre
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.Oral
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.Jehu

© 2022 - 2024 — McMap. All rights reserved.