Redmine之RestApi集成方式
Posted firsttry
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redmine之RestApi集成方式相关的知识,希望对你有一定的参考价值。
Redmine是一个较为强大的开源Ticket管理工具,这篇文章我们将会介绍一下如何使用Curl和Redmine所提供的RestApi来进行集成,将会选取Redmine中的用户进行简单的增删改查的操作。
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 |
安装之后缺省的用户名和密码为:admin/admin,登陆需要修改密码,将其修改为admin/admin123
[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 ~]#
- 1
- 2
- 3
- 4
- 5
启动Rest Web Service
需要在Redmine中做如下设定
详细可参照如下文章:
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 |
这篇文章我们选取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 ~]#
- 1
- 2
- 3
如果需要确认详细的信息,可以使用如下命令, 类似的信息后文不再一一验证。
curl -v -H “Content-Type: application/json” -X GET -u admin:admin123 http://192.168.163.151:3000/users.json
[[email protected] ~]# 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":"[email protected]","created_on":"2017-12-05T12:31:21Z","last_login_on":"2017-12-05T13:06:08Z"}],"total_count":1,"offset":0,"limit":25}[[email protected] ~]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
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 ~]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
执行结果如下,建议打开-v,这样能看到更多信息已进行排错。
[[email protected] ~]# 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":"[email protected]","created_on":"2017-12-05T13:28:53Z","api_key":"4a075c22d3a89bd9ca7c2c0999538a86768ecae3","status":1}}[[email protected] ~]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
再次确认,信息已经得到保存:
[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 ~]#
- 1
- 2
另外使用刚刚创建的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 ~]#
- 1
- 2
- 3
修改对象内容
修改内容为firstname
[root@mail ~]# cat liumiaocn.json
{
"user": {
"login": "liumiaocn",
"firstname": "miaocn"
}
}
[root@mail ~]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
执行更新
[[email protected] ~]# 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
[[email protected] ~]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
结果确认
[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 ~]#
- 1
- 2
- 3
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 ~]#
- 1
- 2
- 3
修改对象内容
修改内容为firstname
[root@mail ~]# cat liumiaocn.json
{
"user": {
"login": "liumiaocn",
"firstname": "miaocn"
}
}
[root@mail ~]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
执行删除
[[email protected] ~]# 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
[[email protected] ~]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
结果确认
[root@mail ~]# curl -X GET -u admin:admin123 http://192.168.163.151:3000/users.json 2>/dev/null |grep liumiaocn
[root@mail ~]#
- 1
- 2
总结
这篇文章我们学习了如何使用curl和restapi以命令行的方式进行集成。
参考文章
http://www.redmine.org/projects/redmine/wiki/Rest_Users
http://www.redmine.org/projects/redmine/wiki/Rest_api_with_curl
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow
以上是关于Redmine之RestApi集成方式的主要内容,如果未能解决你的问题,请参考以下文章