本文翻译自:HTTP POST and GET using cURL in Linux [duplicate]
This question already has an answer here: 这个问题在这里已有答案:
I have a server application written in ASP.NET on Windows that provides a web service. 我在Windows上用ASP.NET编写了一个服务器应用程序,它提供了一个Web服务。
How can I call the web service in Linux with cURL? 如何使用cURL在Linux中调用Web服务?
参考:https://stackoom.com/question/10qZH/在Linux中使用cURL进行HTTP-POST和GET-复制
*nix provides a nice little command which makes our lives a lot easier. * nix提供了一个很好的小命令,使我们的生活更轻松。
GET: 得到:
with JSON: 使用JSON:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource
with XML: 使用XML:
curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource
POST: POST:
For posting data: 用于发布数据:
curl --data "param1=value1¶m2=value2" http://hostname/resource
For file upload: 对于文件上传:
curl --form "[email protected]" http://hostname/resource
RESTful HTTP Post: RESTful HTTP帖子:
curl -X POST -d @filename http://hostname/resource
For logging into a site (auth): 用于登录站点(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: 漂亮印刷卷曲结果:
For JSON: 对于JSON:
If you use npm
and nodejs
, you can install json
package by running this command: 如果使用npm
和nodejs
,则可以通过运行以下命令来安装json
包:
npm install -g json
Usage: 用法:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | json
If you use pip
and python
, you can install pjson
package by running this command: 如果使用pip
和python
,则可以通过运行以下命令来安装pjson
包:
pip install pjson
Usage: 用法:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | pjson
If you use Python 2.6+, json tool is bundled within. 如果您使用Python 2.6+,json工具将捆绑在其中。
Usage: 用法:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | python -m json.tool
If you use gem
and ruby
, you can install colorful_json
package by running this command: 如果使用gem
和ruby
,则可以通过运行以下命令安装colorful_json
包:
gem install colorful_json
Usage: 用法:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | cjson
If you use apt-get
(aptitude package manager of your Linux distro), you can install yajl-tools
package by running this command: 如果您使用apt-get
(Linux发行版的aptitude包管理器),则可以通过运行以下命令来安装yajl-tools
包:
sudo apt-get install yajl-tools
Usage: 用法:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | json_reformat
For XML: 对于XML:
If you use *nix with Debian/Gnome envrionment, install libxml2-utils
: 如果你在Debian / Gnome环境中使用* nix,请安装libxml2-utils
:
sudo apt-get install libxml2-utils
Usage: 用法:
curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource | xmllint --format -
or install tidy
: 或安装tidy
:
sudo apt-get install tidy
Usage: 用法:
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响应保存到文件
curl http://hostname/resource >> /path/to/your/file
or 要么
curl http://hostname/resource -o /path/to/your/file
For detailed description of the curl command, hit: 有关curl命令的详细说明,请点击:
man curl
For details about options/switches of the curl command, hit: 有关curl命令的选项/开关的详细信息,请执行以下操作:
curl -h
I think Amith Koujalgi is correct but also, in cases where the webservice responses are in JSON then it might be more useful to see the results in a clean JSON format instead of a very long string. 我认为Amith Koujalgi是正确的,但是,如果Web服务响应是JSON,那么以干净的JSON格式而不是非常长的字符串查看结果可能更有用。 Just add | 只需添加| grep }| grep} | python -mjson.tool to the end of curl commands here is two examples: python -mjson.tool到curl命令的结尾这里有两个例子:
GET approach with JSON result 使用JSON结果获取GET方法
curl -i -H "Accept: application/json" http://someHostName/someEndpoint | grep }| python -mjson.tool
POST approach with JSON result 使用JSON结果的POST方法
curl -X POST -H "Accept: Application/json" -H "Content-Type: application/json" http://someHostName/someEndpoint -d '{"id":"IDVALUE","name":"Mike"}' | grep }| python -mjson.tool