Redmine之RestApi集成方式
2017-12-05 21:47
459 查看
Redmine是一个较为强大的开源Ticket管理工具,这篇文章我们将会介绍一下如何使用Curl和Redmine所提供的RestApi来进行集成,将会选取Redmine中的用户进行简单的增删改查的操作。
安装之后缺省的用户名和密码为:admin/admin,登陆需要修改密码,将其修改为admin/admin123
详细可参照如下文章:
这篇文章我们选取user相关的api进行集成,可以看出该api已经是stable版本,虽然看起来还有很多Alpha版本,但是在开源领域来说,Redmine已经算是不错的选择。
命令行:curl -X GET -u admin:admin123 http://192.168.163.151:3000/users.json
.json用于制定返回结果的格式
-u制定登陆的用户名和密码
如果需要确认详细的信息,可以使用如下命令, 类似的信息后文不再一一验证。
curl -v -H “Content-Type: application/json” -X GET -u admin:admin123 http://192.168.163.151:3000/users.json
使用json文件方式创建用户,创建用户的json文件信息如下:
执行结果如下,建议打开-v,这样能看到更多信息已进行排错。
再次确认,信息已经得到保存:
另外使用刚刚创建的liumiaocn用户也可以进行登陆了
其中5为用户id, 我们接下来会使用Put方法将firstname从miao改成miaocn。
其中5为用户id, 我们接下来会删除此用户
http://www.redmine.org/projects/redmine/wiki/Rest_api_with_curl
Redmine
相关的介绍以前已经做过,在此不再赘述。详细可以参看如下文章:文章 | 链接 |
---|---|
Ticket管理工具:Redmine | http://blog.csdn.net/liumiaocn/article/details/52107410 |
Bitnami Redmine安装配置指南 | http://blog.csdn.net/liumiaocn/article/details/53523604 |
事前准备
docker方式启动
这篇文章,我们使用easypack下已经整理好的镜像直接使用,使用方法参看如下文章:文章 | 链接 |
---|---|
Ticket管理工具:Redmine | http://blog.csdn.net/liumiaocn/article/details/56254955 |
[root@mail ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 750f29e4deae liumiaocn/redmine "/docker-entrypoin..." 12 minutes ago Up 12 minutes 0.0.0.0:3000->3000/tcp redmine 3230aef19745 liumiaocn/mysql "docker-entrypoint..." 16 minutes ago Up 16 minutes 3306/tcp mysql [root@mail ~]#
启动Rest Web Service
需要在Redmine中做如下设定详细可参照如下文章:
文章 | 链接 |
---|---|
设定方式 | http://blog.csdn.net/liumiaocn/article/details/74832762 |
Rest api
此文中使用的redmine为版本3.3,mysql为5.7, redmine相关的restapi主要如下:Resource | Status | Availability |
---|---|---|
Issues | Stable | 1 |
Projects | Stable | 1 |
Project Memberships | Alpha | 1.4 |
Users | Stable | 1.1 |
Time Entries | Stable | 1.1 |
News | Prototype | 1.1 |
Issue Relations | Alpha | 1.3 |
Versions | Alpha | 1.3 |
Wiki Pages | Alpha | 2.2 |
Queries | Alpha | 1.3 |
Attachments | Beta | 1.3 |
Issue Statuses | Alpha | 1.3 |
Trackers | Alpha | 1.3 |
Enumerations | Alpha | 2.2 |
Issue Categories | Alpha | 1.3 |
Roles | Alpha | 1.4 |
Groups | Alpha | 2.1 |
Custom Fields | Alpha | 2.4 |
Search | Alpha | 3.3 |
Files | Alpha | 3.4 |
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_Usershttp://www.redmine.org/projects/redmine/wiki/Rest_api_with_curl
相关文章推荐
- Confluence集成实践:3:RestAPI集成方式
- Python调用REST API接口的几种方式 推荐
- C# Redmine Rest API使用经验
- Spring和resteasy集成三种方式
- spring和resteasy 的集成方式
- 《Spring Boot极简教程》第14章 Spring Boot集成Swagger2构建自动化Rest API文档
- 百度语音识别REST API使用方法(含JAVA代码)——不需要集成SDK的方法
- 基于Rest的API解决方案(jersey与swagger集成)
- 百度语音识别REST API——通过使用Http网络请求方式获得语音识别功能
- Webix1.1新增了服务器端集成的改善,REST API和XSS安全的支持
- 环信即时通讯-服务器端集成(REST API)-php版demo
- 百度语音识别REST API使用方法(含C++代码)——不需要集成SDK的方法
- Spring和resteasy集成三种方式
- rest api方式实现对文档库的管理
- kafka rest api方式写入——java程序
- Jenkins常见REST API(便于将Jenkins集成到其他系统)
- 百度语音识别REST API——通过使用Http网络请求方式获得语音识别功能
- 通过redmine提供的api自动创建问题以方便与其他工具集成--------同事已验证过
- ActiveMQ_rest API 方式 reload 日志
- 8.5 Spring Boot集成Swagger2构建自动化Rest API文档