專案

一般

個人檔案

動作

使用 cURL 操作 REST API

curl 是一個命令列工具,用於使用各種協定傳輸資料。它可以用於與 Redmine REST API 進行互動。

使用 JSON

以下是一個簡單的命令範例,可用於更新議題

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

發送到 Redmine 的資料檔案(上述範例中的 388.json)如下所示

{
  "issue": {
    "subject": "subject123",
    "notes": "Changing the subject" 
  }
}
注意:您需要根據發送資料的格式設定 Content-Type 標頭。它必須設定為以下值之一
  • application/json
  • application/xml

使用 XML

以下是一個簡單的命令範例,可用於建立帶有自訂欄位的議題

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

其中 issue.xml 內容為

<?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>

文字格式

如果您想使用某些文字格式(例如更新專案上的 wiki 頁面),則應確保使用 curl 的 --data-binary 選項而不是 --data 來載入檔案。只有這樣,curl 才會傳送未變更的換行字元並保留所有格式。

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

其中 wiki.xml 內容為

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

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

附加檔案

如果您想建立帶有 image.png 附加檔案的議題,則需要先上傳此檔案

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

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

然後,使用權杖建立議題

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

其中 issue.xml 內容為

<?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>

由 Toshi MARUYAMA 更新於 將近 5 年 前 · 10 個版本