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

本文翻译自: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服务?


#1楼

参考:https://stackoom.com/question/10qZH/在Linux中使用cURL进行HTTP-POST和GET-复制


#2楼

*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&param2=value2" http://hostname/resource

For file upload: 对于文件上传:

curl --form "fileupload=@filename.txt" 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: 如果使用npmnodejs ,则可以通过运行以下命令来安装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: 如果使用pippython ,则可以通过运行以下命令来安装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: 如果使用gemruby ,则可以通过运行以下命令安装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

#3楼

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

在此输入图像描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值