CURL Command Line URL Parameters

匿名 (未验证) 提交于 2019-12-03 01:03:01

问题:

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?

回答1:

"application/x-www-form-urlencoded" header, why? Try it out:

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

or

curl -X GET 'http://localhost:5000/locations?id=3' 


回答2:

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)

Hope it helps.



回答3:

If your parameter is a number, then you can also do this:

curl -X DELETE localhost:5000/locations/3

Where '3' is your parameter.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!