首页 文章

CURL命令行URL参数

提问于
浏览
119

我正在尝试使用CURL发送带有url参数的 DELETE 请求 . 我在做:

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

但是,服务器没有看到参数id = 3.我尝试使用一些GUI应用程序,当我将URL传递为: http://localhost:5000/locations?id=3 时,它可以工作 . 我真的宁愿使用CURL而不是这个GUI应用程序 . 任何人都可以指出我做错了什么?

2 回答

  • 188

    “application / x-www-form-urlencoded” Headers ,为什么?试试看:

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

    要么

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

    Felipsmartins是对的 .

    值得一提的是,如果这不是POST请求,则无法真正使用-d / - data选项 . 但是如果使用-G选项,这仍然是可能的 .

    这意味着你可以这样做:

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

    这里有点傻但是当你在命令行上并且你有很多参数时,它会更加整洁 .

    我之所以这样说是因为cURL命令通常很长,所以值得在不止一条线路上进行换行 .

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

    如果你使用zsh,这显然更舒服 . 我的意思是当你需要重新编辑上一个命令时,因为zsh允许你逐行进行 . (只是说)

    希望能帮助到你 .

相关问题