REST调试工具

来源:互联网 发布:腾讯数据恢复留言 编辑:程序博客网 时间:2024/05/21 09:41

兵马未动,粮草先行。
随着对jersey的学习,这段时间也对REST的调试工具也有了一些了解

1.命令行调试工具crul

curl在REST中的使用

curl是非常易用、强大的基于url标准的命令行工具,通过命令行即可完成多种协议的请求,并可以将请求的信息输出在终端/控制台上。它支持文件上传和下载,所以是综合传输工具。因此对于调试和测试REST请求非常方便。利用curl指令,可以送出HTTP GET, POST, PUT, DELETE, 也可以改变HTTP header来满足使用REST API需要的特定条件。
下载地址:https://curl.haxx.se/download.html 可以根据自己的操作系统下载对应的版本。
下面列出目前测试REST时常用到的参数:

命令 描述 -X/–request [GET POST PUT DELETE …] 使用指定的http method发出 http request -H/–header 设定request里的header -i/–include 显示response的header -d/–data 设定 http parameters -v/–verbose 输出比较多的信息 -u/–user 使用者账号、密码 -b/–cookie cookie

linux command line 的參數常,同一个功能常会有两个功能完全相同参数,一个是比较短的参数,前面通常是用-(一个-)符号,另一个比较长的参数,通常会用–(两个-)符号。
参数-X跟–request两个功能是一样的,所以使用时ex:curl -X POST “http://www.example.com/” 跟 curl –request POST “http://www.example.com/” 功能是相等的。

-d/–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.

如过需要在request时同时传送parameter跟json,parameter可以加在url后面,json数据则放入-d的参数,然后利用单引号将json字符串含起来(如果json內容是用单引号,-d的参数改用双引号引起来),header要加入”Content-Type:application/json”跟”Accept:application/json”

curl http://www.example.com?modifier=kent -X PUT -i -H "Content-Type:application/json" -H "Accept:application/json" -d '{"boolean" : false, "foo" : "bar"}'# 不加"Accept:application/json"也可以curl http://www.example.com?modifier=kent -X PUT -i -H "Content-Type:application/json" -d '{"boolean" : false, "foo" : "bar"}'

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
curl -H “Content-Type:application/xml” -d @filename.xml url

curl中使用一组url作为请求地址的示例如下:

http://site.{one,two,three}.comftp://numericals.com/file[1-10].txt#1到100中每隔10发一次,如:1,11,21,31...http://www.numericals.com/file[1-100:10].txthttp://any.org/archive[1996-1999]/vol[1-4]part{a,b,c}.html

curl其他的一些使用

使用-L选项进行重定向

1 # 让curl使用地址重定向,此时会查询google.com.hk站点2 curl -L http://www.google.com

通过-o/-O选项保存下载的文件到指定的文件中:
-o:将文件保存为命令行中指定的文件名的文件中
-O:使用URL中默认的文件名保存文件到本地

1 # 将文件下载到本地并命名为mygettext.html2 curl -o mygettext.html http://www.gnu.org/software/gettext/manual/gettext.html3 4 # 将文件保存到本地并命名为gettext.html5 curl -O http://www.gnu.org/software/gettext/manual/gettext.html

断点续传
通过使用-C选项可对大文件使用断点续传功能,如:

1 # 当文件在下载完成之前结束该进程2 $ curl -O http://www.gnu.org/software/gettext/manual/gettext.html3 ##############             20.1%4 5 # 通过添加-C选项继续对该文件进行下载,已经下载过的文件不会被重新下载6 curl -C - -O http://www.gnu.org/software/gettext/manual/gettext.html7 ###############            21.1%

对CURL使用网络限速
通过–limit-rate选项对CURL的最大网络使用进行限制

1 # 下载速度最大不会超过1000B/second2 3 curl --limit-rate 1000B -O http://www.gnu.org/software/gettext/manual/gettext.html

从FTP服务器下载文件
CURL同样支持FTP下载,若在url中指定的是某个文件路径而非具体的某个要下载的文件名,CURL则会列出该目录下的所有文件名而并非下载该目录下的所有文件

1 # 列出public_html下的所有文件夹和文件2 curl -u ftpuser:ftppass -O ftp://ftp_server/public_html/3 4 # 下载xss.php文件5 curl -u ftpuser:ftppass -O ftp://ftp_server/public_html/xss.php

上传文件到FTP服务器
通过 -T 选项可将指定的本地文件上传到FTP服务器上

# 将myfile.txt文件上传到服务器curl -u ftpuser:ftppass -T myfile.txt ftp://ftp.testserver.com# 同时上传多个文件curl -u ftpuser:ftppass -T "{file1,file2}" ftp://ftp.testserver.com# 从标准输入获取内容保存到服务器指定的文件中curl -u ftpuser:ftppass -T - ftp://ftp.testserver.com/myfile_1.txt

2.基于浏览器的图形化调试插件

Simple REST Client插件

Simple REST Client插件是基于Chrome浏览器的扩展,安装该插件后Chrome窗口的右上方会出现该插件的图标。插件的下载地址是:http://chrome.google.com/extensions/detail/fhjcajmcbmldlhcimfajhfbgofnpcjmb.该插件的界面如下所示:
这里写图片描述

0 0
原创粉丝点击