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

 

 

 

你可能感兴趣的:(html,xml,浏览器,REST)