首页 文章

在Linux中使用cURL进行HTTP POST和GET [复制]

提问于
浏览
304

这个问题在这里已有答案:

我在windows中的asp.net中有服务器应用程序,因为我有一个Web服务 .

如何使用cURL命令使用shell脚本在Linux中调用Web服务?

2 回答

  • 613
    • nix提供了一个很好的小命令,使我们的生活更轻松 .

    GET:

    使用JSON:

    curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource
    

    使用XML:

    curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource
    

    POST:

    发布数据:

    curl --data "param1=value1&param2=value2" http://hostname/resource
    

    对于文件上传:

    curl --form "fileupload=@filename.txt" http://hostname/resource
    

    RESTful HTTP帖子:

    curl -X POST -d @filename http://hostname/resource
    

    用于登录站点(auth):

    curl -d "username=admin&password=admin&submit=Login" --dump-header headers http://localhost/Login
    curl -L -b headers http://localhost/
    

    Pretty-printing the curl results:

    对于JSON:

    如果使用 npmnodejs ,则可以通过运行以下命令安装 json 包:

    npm install -g json
    

    用法:

    curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | json
    

    如果使用 pippython ,则可以通过运行以下命令安装 pjson package:

    pip install pjson
    

    用法:

    curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | pjson
    

    如果您使用Python 2.6,json工具将捆绑在其中 .

    用法:

    curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | python -m json.tool
    

    如果使用 gemruby ,则可以通过运行以下命令安装 colorful_json 包:

    gem install colorful_json
    

    用法:

    curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | cjson
    

    如果使用 apt-get (Linux发行版的aptitude包管理器),则可以通过运行以下命令安装 yajl-tools 包:

    sudo apt-get install yajl-tools
    

    用法:

    curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource |  json_reformat
    

    对于XML:

    如果你在Debian / Gnome环境中使用* nix,请安装 libxml2-utils

    sudo apt-get install libxml2-utils
    

    用法:

    curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource | xmllint --format -
    

    或者安装 tidy

    sudo apt-get install tidy
    

    用法:

    curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource | tidy -xml -i -
    

    Saving the curl response to a file

    curl http://hostname/resource >> /path/to/your/file
    

    要么

    curl http://hostname/resource -o /path/to/your/file
    

    有关curl命令的详细说明,请点击:

    man curl
    

    有关curl命令的选项/开关的详细信息,请执行以下操作:

    curl -h
    
  • 48

    我认为Amith Koujalgi是正确的,但是,如果Web服务响应是JSON,那么以干净的JSON格式而不是非常长的字符串查看结果可能更有用 . 只需添加| grep} | python -mjson.tool到curl命令的结尾这里有两个例子:

    GET approach with JSON result

    curl -i -H "Accept: application/json" http://someHostName/someEndpoint | grep }| python -mjson.tool
    

    POST approach with JSON result

    curl -X POST  -H "Accept: Application/json" -H "Content-Type: application/json" http://someHostName/someEndpoint -d '{"id":"IDVALUE","name":"Mike"}' | grep }| python -mjson.tool
    

    enter image description here

相关问题