这个问题在这里已经有了答案:How to send a header using a HTTP request through a cURL call? (11 个回答) 6 年前关闭。
我有一个在 Windows 上用 ASP.NET 编写的服务器应用程序,它提供 Web 服务。
如何使用 cURL 在 Linux 中调用 Web 服务?
*nix 提供了一个不错的小命令,它让我们的生活更轻松。
得到:
使用 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
邮政:
对于发布数据:
curl --data "param1=value1¶m2=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/
漂亮地打印卷曲结果:
对于 JSON:
如果您使用 npm
和 nodejs
,您可以通过运行以下命令来安装 json
包:
npm install -g json
用法:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | json
如果您使用 pip
和 python
,您可以通过运行以下命令来安装 pjson
包:
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
如果您使用 gem
和 ruby
,您可以通过运行以下命令来安装 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 -
将 curl 响应保存到文件中
curl http://hostname/resource >> /path/to/your/file
或者
curl http://hostname/resource -o /path/to/your/file
有关 curl 命令的详细说明,请点击:
man curl
有关 curl 命令的选项/开关的详细信息,请点击:
curl -h
我认为 Amith Koujalgi 是正确的,但如果 web 服务响应是 JSON 格式,那么以干净的 JSON 格式而不是很长的字符串查看结果可能更有用。只需添加 | grep }| python -mjson.tool 到 curl 命令的结尾这里是两个例子:
带有 JSON 结果的 GET 方法
curl -i -H "Accept: application/json" http://someHostName/someEndpoint | grep }| python -mjson.tool
带有 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
https://i.stack.imgur.com/T2FqW.png
curl -s -H "Accept: application/json" -H "Content-Type: application/json" -H "http://${API_BASE_URL}${1}" | python -mjson.tool
不定期副业成功案例分享
less
,则可能需要 add-sS
。less
。get
示例中,您可以引用整个 url 以避免参数错误,例如curl "http://www.virustotal.com/vtapi/v2/ip-address/report?ip=8.8.8.8&apikey=1233456890"