在 Linux 中使用 cURL 的 HTTP POST 和 GET

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14978411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:04:54  来源:igfitidea点击:

HTTP POST and GET using cURL in Linux

linuxweb-servicescurl

提问by Randhi Rupesh

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 服务?

回答by Amith Koujalgi

*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:

邮政:

For posting data:

对于发布数据:

curl --data "param1=value1&param2=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):

登录站点(身份验证):

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 npmand nodejs, you can install jsonpackage by running this command:

如果使用npmand 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 pipand python, you can install pjsonpackage by running this command:

如果使用pipand 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 gemand ruby, you can install colorful_jsonpackage by running this command:

如果使用gemand 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-toolspackage 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

回答by grepit

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. Just add | grep }| python -mjson.tool to the end of curl commands here is two examples:

我认为 Amith Koujalgi 是正确的,但在 Web 服务响应是 JSON 的情况下,以干净的 JSON 格式而不是很长的字符串查看结果可能更有用。只需添加 | grep }| 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

enter image description here

在此处输入图片说明