通过curl访问rest接口


curl is a command-line tool for transferring data using various protocols. It can be used to interact with the Redmine REST API.

Using JSON

Here is a simple example of a command that can be used to update an issue:

curl -v -H "Content-Type: application/json" -X PUT --data "@388.json" -u login:password http://redmine/issues/388.json
curl -v -H "Content-Type: application/json" -X PUT --data "@388.json" -H "X-Redmine-API-Key: xxxx" http://redmine/issues/388.json

The file that contains the data sent to Redmine (388.json in the example above) would look like this:

{
  "issue": {
    "subject": "subject123",
    "notes": "Changing the subject" 
  }
}
Note: it's required to set the  Content-Type  header according to the format of the data you are sending. It must be set to one the following values:
  • application/json
  • application/xml

Using XML

Here is a simple example of a command that can be used to create an issue with custom fields:

curl -v -H "Content-Type: application/xml" -X POST --data "@issue.xml" -u login:password http://redmine/issues.xml
curl -v -H "Content-Type: application/xml" -X POST --data "@issue.xml" -H "X-Redmine-API-Key: xxxx" http://redmine/issues.xml

Where issue.xml content is:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<issue>
  <subject>API custom fields</subject>
  <project_id>1</project_id>
  <tracker_id>2</tracker_id>
  <custom_fields type="array">
    <custom_field>
      <id>2</id>
      <value>Fixed</value>
    </custom_field>
    <custom_field>
      <id>1</id>
      <value>0.8.2</value>
    </custom_field>
  </custom_fields>
</issue>

Text formatting

If you want to use some text formatting (e.g to update a wiki page on your project), you should use the curl's option --data-binary instead of --data to load the file.

curl -v -H "Content-Type: application/xml" -X PUT --data-binary "@wiki.xml" -u login:password http://redmine/projects/foo/wiki/page_test.xml

Where wiki.xml content is:

<?xml version="1.0"?>
<wiki_page>
<text>
h1. TITLE

 %{font-size:14pt}SUBTITLE%
</text>
</wiki_page>

Attaching files

If you want to create an issue with image.png attached, you need to upload this file first:

curl --data-binary "@image.png" -H "Content-Type: application/octet-stream" -X POST -u login:password http://redmine/uploads.xml

# 201 response
<upload>
  <token>7167.ed1ccdb093229ca1bd0b043618d88743</token>
</upload>

Then, use the token to create the issue:

curl -v -H "Content-Type: application/xml" -X POST --data "@issue.xml" -u login:password http://redmine/issues.xml

Where issue.xml content is:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<issue>
  <subject>Issue with attachment</subject>
  <project_id>1</project_id>
  <uploads type="array">
    <upload>
      <token>7167.ed1ccdb093229ca1bd0b043618d88743</token>
      <filename>image.png</filename>
      <content_type>image/png</content_type>
    </upload>
  </uploads>
</issue>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值