Curl用法点滴

CURL-命令行浏览器 Curl非常强大,完全可以作为一个REST CLIENT端的工具来使用,而且非常方便快捷。

 

1. Post 方法

1.1 如果在REST 服务器一端,定义了一个addAccount方法,而且这个方法是通过Post方式发送的,服务器端以QueryParam方式接受参数。

 

@POST
    public Response addAccount(@QueryParam("username") String name, @QueryParam("password");

 

那么用Curl来进行Rest 调用时如何传递参数呢?

 

curl -X POST  "url?username=xiaowang&password=123"

 

注意:1)传递了两个参数,所以url后面要用双引号。

        2) -X 的使用,如果不是用-X,默认是以GET方式发送请求的,如果想用其他方法,需加上-X参数,同时,后面跟上方法名称,如POST/PUT/DELETE。

 

下面一段话是http://curl.haxx.se/docs/manpage.html上面对-X参数的解释。

-X/--request <command>

(HTTP) Specifies a custom request method to use when communicating with the HTTP server. The specified request will be used instead of the method otherwise used (which defaults to GET).

 

 

1.2. 如果在REST 服务器一端,定义了一个addAccount方法,而且这个方法是通过Post方式发送的,服务器端以FormParam

方式接受参数。

 

@POST
    public Response addAccount(@FormParam ("username") String name, @FormParam ("password");

 

Curl调用Rest Service方法如下:

curl -d "username=xiaowang&password=123"  url

 

-d选项表示此方法为Post方法,传送的数据是-d后面的内容。此例和在HTML Form中输入username/password并提交有着相同的效果。

 

1.3展示了如何Post一个文件的内容到服务器上。关键是使用在-d参数后面跟@filename。

 

-d/--data <data> (HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded

 

If you start the data with the letter @, the rest should be a file name to read the data from, or - if you want curl to read the data from stdin. The contents of the file must already be URL-encoded. Multiple files can also be specified. Posting data from a file named 'foobar' would thus be done with --data @foobar .

 

1.3 .如何Post一个文件到服务器端呢?

 

curl -H "Content-Type:application/xml" -d @filename.xml url

 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值