在Linux中使用cURL的HTTP POST和GET [英] HTTP POST and GET using cURL in Linux

查看:157
本文介绍了在Linux中使用cURL的HTTP POST和GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

解决方案

Linux提供了一个很好的小命令,使我们的生活更容易。



GET:




  curl -i -HAccept:application / json-HContent-Type:application / json-X GET http:// hostname / resource 

  curl -HAccept:application / xml-HContent-Type:application / xml-X GET http:// hostname / resource 



POST:



发布数据:

  curl --dataparam1 = value1& param2 = value2http:// hostname /资源

对于文件上传:

  curl --formfileupload=@filename.txthttp:// hostname / resource 

RESTful HTTP POST:



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

(auth):

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

漂亮打印卷曲结果



对于JSON: b
$ b

如果您使用 npm nodejs ,您可以安装 json 运行此命令:

  npm install -g json 

用法:

  curl -i -HAccept:application / json-HContent-Type:application / json-X GET http:// hostname / resource | json 



如果使用 pip python ,您可以通过运行以下命令安装 pjson 包:

  pip install pjson 

用法:

  curl -i -HAccept:application / json-HContent-Type:application / json-X GET http:// hostname /资源| pjson 

如果你使用Python 2.6+,json工具捆绑在一起。



用法:

  curl -i -H接受:application / json-H -Type:application / json-X GET http:// hostname / resource | python -m json.tool 

如果使用 gem ruby​​ ,您可以通过运行以下命令安装 colorful_json 包:

  gem install colorful_json 

/ p>

  curl -i -HAccept:application / json-HContent-Type:application / json-X GET http :// hostname / resource | cjson 



如果使用 apt-get 通过运行以下命令,您可以安装 yajl-tools 软件包:

  sudo apt-get install yajl-tools 

用法:

  curl -i -HAccept:application / json-HContent-Type:application / json-X GET http: / hostname / resource | json_reformat 

对于XML:



如果您使用带有Debian / Gnome环境的Linux,请安装 libxml2-utils

  sudo apt-get install libxml2-utils 



  curl -HAccept:application / xml-HContent-Type:application / xml-X GET http:// hostname /资源| xmllint --format  -  

或安装 tidy

  sudo apt-get install tidy 

用法:

  curl -H接受:application / xml-H -Type:application / xml-X GET http:// hostname / resource | tidy -xml -i  -  

将curl响应保存到文件 / p>

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

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

有关curl命令的详细描述,请点击:

  man curl 

有关curl命令的选项/开关的详细信息,请键入:

  curl -h 


I had server application in asp.net in windows in that I had a web service for that.

How can I call web service in Linux using shell script by using cURL command?

解决方案

Linux provides a nice little command which makes our lives a lot easier.

GET:

with JSON:

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

with 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 "fileupload=@filename.txt" http://hostname/resource

RESTful HTTP Post:

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:

If you use npm and nodejs, you can install json package by running this command:

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 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.

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

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:

If you use Linux with Debian/Gnome envrionment, install 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:

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

man curl

For details about options/switches of the curl command, hit:

curl -h

这篇关于在Linux中使用cURL的HTTP POST和GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆