Redmine之RestApi集成方式
来源:互联网 发布:linux cpu 超过100 编辑:程序博客网 时间:2024/06/05 07:20
Redmine是一个较为强大的开源Ticket管理工具,这篇文章我们将会介绍一下如何使用Curl和Redmine所提供的RestApi来进行集成,将会选取Redmine中的用户进行简单的增删改查的操作。
Redmine
相关的介绍以前已经做过,在此不再赘述。详细可以参看如下文章:
事前准备
docker方式启动
这篇文章,我们使用easypack下已经整理好的镜像直接使用,使用方法参看如下文章:
安装之后缺省的用户名和密码为:admin/admin,登陆需要修改密码,将其修改为admin/admin123
[root@mail ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES750f29e4deae liumiaocn/redmine "/docker-entrypoin..." 12 minutes ago Up 12 minutes 0.0.0.0:3000->3000/tcp redmine3230aef19745 liumiaocn/mysql "docker-entrypoint..." 16 minutes ago Up 16 minutes 3306/tcp mysql[root@mail ~]#
启动Rest Web Service
需要在Redmine中做如下设定
详细可参照如下文章:
Rest api
此文中使用的redmine为版本3.3,mysql为5.7, redmine相关的restapi主要如下:
这篇文章我们选取user相关的api进行集成,可以看出该api已经是stable版本,虽然看起来还有很多Alpha版本,但是在开源领域来说,Redmine已经算是不错的选择。
Http Get:查询
首先使用Get 方法进行查询:
命令行:curl -X GET -u admin:admin123 http://192.168.163.151:3000/users.json
.json用于制定返回结果的格式
-u制定登陆的用户名和密码
[root@mail ~]# curl -X GET -u admin:admin123 http://192.168.163.151:3000/users.json{"users":[{"id":1,"login":"admin","firstname":"Redmine","lastname":"Admin","mail":"admin@example.net","created_on":"2017-12-05T12:31:21Z","last_login_on":"2017-12-05T13:02:36Z"}],"total_count":1,"offset":0,"limit":25}[root@mail ~]# [root@mail ~]#
如果需要确认详细的信息,可以使用如下命令, 类似的信息后文不再一一验证。
curl -v -H “Content-Type: application/json” -X GET -u admin:admin123 http://192.168.163.151:3000/users.json
[root@mail ~]# curl -v -H "Content-Type: application/json" -X GET -u admin:admin123 http://192.168.163.151:3000/users.json* About to connect() to 192.168.163.151 port 3000 (#0)* Trying 192.168.163.151...* Connected to 192.168.163.151 (192.168.163.151) port 3000 (#0)* Server auth using Basic with user 'admin'> GET /users.json HTTP/1.1> Authorization: Basic YWRtaW46YWRtaW4xMjM=> User-Agent: curl/7.29.0> Host: 192.168.163.151:3000> Accept: */*> Content-Type: application/json> < HTTP/1.1 200 OK < X-Frame-Options: SAMEORIGIN< X-Xss-Protection: 1; mode=block< X-Content-Type-Options: nosniff< Content-Type: application/json; charset=utf-8< Content-Length: 217< Etag: W/"1e49c564bd63f382cd5b6c3697d786a3"< Cache-Control: max-age=0, private, must-revalidate< X-Request-Id: b3f10c08-37bd-48dd-a455-24c5944bea3d< X-Runtime: 0.187073< Server: WEBrick/1.3.1 (Ruby/2.3.3/2016-11-21)< Date: Tue, 05 Dec 2017 13:06:08 GMT< Connection: Keep-Alive< Set-Cookie: _redmine_session=WHY1UGNwdHI4VE8wYXk5SjlRTk15YkYyU2RSZXUrZDRPMllnVFBReExoTGxBN0lhRnF6ZVI2WDR4YkI2Z0Y4M3N2Uk1OOURBYVJTNmQ0YVhLV2F1aGdxdjdTcE5MQUhRNFdQRXpLMTdmVU1UK2RaTkY0L0F4WEk0WldaRjAxVW5WTHNOQ2FQNElYeURkNlA0bTYzaGNRPT0tLUVSQ3RNMmtHYXhhdE44TzdxbmM0VWc9PQ%3D%3D--cbb59aa7ef006f47dbefe320b6121e2605e43f66; path=/; HttpOnly< * Connection #0 to host 192.168.163.151 left intact{"users":[{"id":1,"login":"admin","firstname":"Redmine","lastname":"Admin","mail":"admin@example.net","created_on":"2017-12-05T12:31:21Z","last_login_on":"2017-12-05T13:06:08Z"}],"total_count":1,"offset":0,"limit":25}[root@mail ~]#
Http Post : 新增用户
使用命令:curl -v -H “Content-Type: application/json” -X POST –data-binary “@liumiaocn.json” -u admin:admin123 http://192.168.163.151:3000/users.json
使用json文件方式创建用户,创建用户的json文件信息如下:
[root@mail ~]# cat liumiaocn.json { "user": { "login": "liumiaocn", "firstname": "miao", "lastname": "liu", "mail": "liumiaocn@outlook.com", "password": "hello123" }}[root@mail ~]#
执行结果如下,建议打开-v,这样能看到更多信息已进行排错。
[root@mail ~]# curl -v -H "Content-Type: application/json" -X POST --data-binary "@liumiaocn.json" -u admin:admin123 http://192.168.163.151:3000/users.json* About to connect() to 192.168.163.151 port 3000 (#0)* Trying 192.168.163.151...* Connected to 192.168.163.151 (192.168.163.151) port 3000 (#0)* Server auth using Basic with user 'admin'> POST /users.json HTTP/1.1> Authorization: Basic YWRtaW46YWRtaW4xMjM=> User-Agent: curl/7.29.0> Host: 192.168.163.151:3000> Accept: */*> Content-Type: application/json> Content-Length: 183> * upload completely sent off: 183 out of 183 bytes< HTTP/1.1 201 Created < X-Frame-Options: SAMEORIGIN< X-Xss-Protection: 1; mode=block< X-Content-Type-Options: nosniff< Location: http://192.168.163.151:3000/users/5< Content-Type: application/json; charset=utf-8< Content-Length: 204< Etag: W/"7cbd19185b3fd02c67cc3e22ec9ab1d7"< Cache-Control: max-age=0, private, must-revalidate< X-Request-Id: 458e9f6e-e9c3-41ff-95a6-fefeb262ab9d< X-Runtime: 0.736546< Server: WEBrick/1.3.1 (Ruby/2.3.3/2016-11-21)< Date: Tue, 05 Dec 2017 13:28:53 GMT< Connection: Keep-Alive< * Connection #0 to host 192.168.163.151 left intact{"user":{"id":5,"login":"liumiaocn","firstname":"miao","lastname":"liu","mail":"liumiaocn@outlook.com","created_on":"2017-12-05T13:28:53Z","api_key":"4a075c22d3a89bd9ca7c2c0999538a86768ecae3","status":1}}[root@mail ~]#
再次确认,信息已经得到保存:
[root@mail ~]# curl -X GET -u admin:admin123 http://192.168.163.151:3000/users.json{"users":[{"id":1,"login":"admin","firstname":"Redmine","lastname":"Admin","mail":"admin@example.net","created_on":"2017-12-05T12:31:21Z","last_login_on":"2017-12-05T13:31:47Z"},{"id":5,"login":"liumiaocn","firstname":"miao","lastname":"liu","mail":"liumiaocn@outlook.com","created_on":"2017-12-05T13:28:53Z"}],"total_count":2,"offset":0,"limit":25}[root@mail ~]#
另外使用刚刚创建的liumiaocn用户也可以进行登陆了
Http Put : 用户修改
使用命令:curl -v -H “Content-Type: application/json” -X PUT –data-binary “@liumiaocn.json” -u admin:admin123 http://192.168.163.151:3000/users/5.json
其中5为用户id, 我们接下来会使用Put方法将firstname从miao改成miaocn。
事前确认
[root@mail ~]# curl -X GET -u admin:admin123 http://192.168.163.151:3000/users.json 2>/dev/null |grep liumiaocn{"users":[{"id":1,"login":"admin","firstname":"Redmine","lastname":"Admin","mail":"admin@example.net","created_on":"2017-12-05T12:31:21Z","last_login_on":"2017-12-05T13:39:16Z"},{"id":5,"login":"liumiaocn","firstname":"miao","lastname":"liu","mail":"liumiaocn@outlook.com","created_on":"2017-12-05T13:28:53Z","last_login_on":"2017-12-05T13:33:47Z"}],"total_count":2,"offset":0,"limit":25}[root@mail ~]#
修改对象内容
修改内容为firstname
[root@mail ~]# cat liumiaocn.json { "user": { "login": "liumiaocn", "firstname": "miaocn" }}[root@mail ~]#
执行更新
[root@mail ~]# curl -v -H "Content-Type: application/json" -X PUT --data-binary "@liumiaocn.json" -u admin:admin123 http://192.168.163.151:3000/users/5.json* About to connect() to 192.168.163.151 port 3000 (#0)* Trying 192.168.163.151...* Connected to 192.168.163.151 (192.168.163.151) port 3000 (#0)* Server auth using Basic with user 'admin'> PUT /users/5.json HTTP/1.1> Authorization: Basic YWRtaW46YWRtaW4xMjM=> User-Agent: curl/7.29.0> Host: 192.168.163.151:3000> Accept: */*> Content-Type: application/json> Content-Length: 84> * upload completely sent off: 84 out of 84 bytes< HTTP/1.1 200 OK < X-Frame-Options: SAMEORIGIN< X-Xss-Protection: 1; mode=block< X-Content-Type-Options: nosniff< Content-Type: application/json; charset=utf-8< Content-Length: 0< Cache-Control: no-cache< X-Request-Id: 690f8890-5651-4c11-bad3-d854a10c926e< X-Runtime: 0.175155< Server: WEBrick/1.3.1 (Ruby/2.3.3/2016-11-21)< Date: Tue, 05 Dec 2017 13:39:43 GMT< Connection: Keep-Alive< * Connection #0 to host 192.168.163.151 left intact[root@mail ~]#
结果确认
[root@mail ~]# curl -X GET -u admin:admin123 http://192.168.163.151:3000/users.json 2>/dev/null |grep liumiaocn{"users":[{"id":1,"login":"admin","firstname":"Redmine","lastname":"Admin","mail":"admin@example.net","created_on":"2017-12-05T12:31:21Z","last_login_on":"2017-12-05T13:39:56Z"},{"id":5,"login":"liumiaocn","firstname":"miaocn","lastname":"liu","mail":"liumiaocn@outlook.com","created_on":"2017-12-05T13:28:53Z","last_login_on":"2017-12-05T13:33:47Z"}],"total_count":2,"offset":0,"limit":25}[root@mail ~]#
Http Delete
使用命令:curl -v -H “Content-Type: application/json” -X DELETE –data-binary “@liumiaocn.json” -u admin:admin123 http://192.168.163.151:3000/users/5.json
其中5为用户id, 我们接下来会删除此用户
事前确认
[root@mail ~]# curl -X GET -u admin:admin123 http://192.168.163.151:3000/users.json 2>/dev/null |grep liumiaocn{"users":[{"id":1,"login":"admin","firstname":"Redmine","lastname":"Admin","mail":"admin@example.net","created_on":"2017-12-05T12:31:21Z","last_login_on":"2017-12-05T13:43:29Z"},{"id":5,"login":"liumiaocn","firstname":"miaocn","lastname":"liu","mail":"liumiaocn@outlook.com","created_on":"2017-12-05T13:28:53Z","last_login_on":"2017-12-05T13:33:47Z"}],"total_count":2,"offset":0,"limit":25}[root@mail ~]#
修改对象内容
修改内容为firstname
[root@mail ~]# cat liumiaocn.json { "user": { "login": "liumiaocn", "firstname": "miaocn" }}[root@mail ~]#
执行删除
[root@mail ~]# curl -v -H "Content-Type: application/json" -X DELETE --data-binary "@liumiaocn.json" -u admin:admin123 http://192.168.163.151:3000/users/5.json* About to connect() to 192.168.163.151 port 3000 (#0)* Trying 192.168.163.151...* Connected to 192.168.163.151 (192.168.163.151) port 3000 (#0)* Server auth using Basic with user 'admin'> DELETE /users/5.json HTTP/1.1> Authorization: Basic YWRtaW46YWRtaW4xMjM=> User-Agent: curl/7.29.0> Host: 192.168.163.151:3000> Accept: */*> Content-Type: application/json> Content-Length: 84> * upload completely sent off: 84 out of 84 bytes< HTTP/1.1 200 OK < X-Frame-Options: SAMEORIGIN< X-Xss-Protection: 1; mode=block< X-Content-Type-Options: nosniff< Content-Type: application/json; charset=utf-8< Content-Length: 0< Cache-Control: no-cache< X-Request-Id: 016ec5d3-eef4-4db9-8813-28a21d0aa8c2< X-Runtime: 0.453139< Server: WEBrick/1.3.1 (Ruby/2.3.3/2016-11-21)< Date: Tue, 05 Dec 2017 13:43:49 GMT< Connection: Keep-Alive< * Connection #0 to host 192.168.163.151 left intact[root@mail ~]#
结果确认
[root@mail ~]# curl -X GET -u admin:admin123 http://192.168.163.151:3000/users.json 2>/dev/null |grep liumiaocn[root@mail ~]#
总结
这篇文章我们学习了如何使用curl和restapi以命令行的方式进行集成。
参考文章
http://www.redmine.org/projects/redmine/wiki/Rest_Users
http://www.redmine.org/projects/redmine/wiki/Rest_api_with_curl
- Redmine之RestApi集成方式
- Confluence集成实践:3:RestAPI集成方式
- Confluence集成实践:2:Confluence的RestAPI
- redmine 集成 git
- redmine与git集成
- redmine 集成 git
- redmine 和 github 集成
- 持续集成之jenkins实践教程:基础篇(4): 集成redmine
- 极光推送Jpush之RestAPI使用实例
- Linux - Redmine使用方式
- Testlink与Redmine的集成
- Redmine与Gitlab深度集成
- redmine 之yaml语言
- redmine 安装之旅
- Ubuntu 安装 Redmine 并集成 git
- jenkins 集成 redmine 账户验证的方案
- redmine与svn完美集成方案
- redmine与jenkins完美集成方案
- 程序设计入门1 分支选择三整数排序
- 51nod 1674 区间的价值 V2
- 面向对象编程基础
- 数据结构实验之图论二:图的深度遍历
- 对象和实例,对象的创建
- Redmine之RestApi集成方式
- hdu5446(数论)
- CSR 蓝牙读写
- UI5开发 – 发布到Fiori LaunchPad以及调试
- 单向循环链表的实现
- js中创建元素createElement\createTextNode\appendChild
- MATLAB中生成随机数方法总结
- shiro-03shiro的filterChainDefinitions
- UI5开发 – 解决Fiori LaunchPad不能加载Google API问题