Rest api with curl » History » Version 3

Jean-Philippe Lang, 2011-07-03 19:55
XML example with custom fields

1 1 Jean-Philippe Lang
h1. Using the REST API with cURL
2 1 Jean-Philippe Lang
3 1 Jean-Philippe Lang
"curl":http://curl.haxx.se/ is a command-line tool for transferring data using various protocols. It can be used to interact with the Redmine REST API.
4 1 Jean-Philippe Lang
5 3 Jean-Philippe Lang
h2. Using JSON
6 3 Jean-Philippe Lang
7 1 Jean-Philippe Lang
Here is a simple example of a command that can be used to update an issue:
8 1 Jean-Philippe Lang
9 1 Jean-Philippe Lang
<pre>
10 3 Jean-Philippe Lang
curl -v -H "Content-Type: application/json" -X PUT --data "@388.json" -u login:password http://redmine/issues/388.json
11 1 Jean-Philippe Lang
</pre>
12 1 Jean-Philippe Lang
13 1 Jean-Philippe Lang
The file that contains the data sent to Redmine (388.json in the example above) would look like this:
14 1 Jean-Philippe Lang
15 1 Jean-Philippe Lang
<pre>
16 1 Jean-Philippe Lang
{
17 1 Jean-Philippe Lang
  "issue": {
18 1 Jean-Philippe Lang
    "subject": "subject123",
19 1 Jean-Philippe Lang
    "notes": "Changing the subject"
20 1 Jean-Philippe Lang
  }
21 1 Jean-Philippe Lang
}
22 1 Jean-Philippe Lang
</pre>
23 2 Jean-Philippe Lang
24 2 Jean-Philippe Lang
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:
25 2 Jean-Philippe Lang
* @application/json@
26 1 Jean-Philippe Lang
* @application/xml@
27 3 Jean-Philippe Lang
28 3 Jean-Philippe Lang
h2. Using XML
29 3 Jean-Philippe Lang
30 3 Jean-Philippe Lang
31 3 Jean-Philippe Lang
Here is a simple example of a command that can be used to create an issue with custom fields:
32 3 Jean-Philippe Lang
33 3 Jean-Philippe Lang
<pre>
34 3 Jean-Philippe Lang
curl -v -H "Content-Type: application/xml" -X POST --data "@issue.xml" -u login:password http://redmine/issues.xml
35 3 Jean-Philippe Lang
</pre>
36 3 Jean-Philippe Lang
37 3 Jean-Philippe Lang
Where issue.xml content is:
38 3 Jean-Philippe Lang
39 3 Jean-Philippe Lang
<pre>
40 3 Jean-Philippe Lang
<?xml version="1.0" encoding="ISO-8859-1" ?>
41 3 Jean-Philippe Lang
<issue>
42 3 Jean-Philippe Lang
  <subject>API custom fields</subject>
43 3 Jean-Philippe Lang
  <project_id>1</project_id>
44 3 Jean-Philippe Lang
  <tracker_id>2</tracker_id>
45 3 Jean-Philippe Lang
  <custom_fields type="array">
46 3 Jean-Philippe Lang
    <custom_field>
47 3 Jean-Philippe Lang
      <id>2</id>
48 3 Jean-Philippe Lang
      <value>Fixed</value>
49 3 Jean-Philippe Lang
    </custom_field>
50 3 Jean-Philippe Lang
    <custom_field>
51 3 Jean-Philippe Lang
      <id>1</id>
52 3 Jean-Philippe Lang
      <value>0.8.2</value>
53 3 Jean-Philippe Lang
    </custom_field>
54 3 Jean-Philippe Lang
  </custom_fields>
55 3 Jean-Philippe Lang
</issue>
56 3 Jean-Philippe Lang
</pre>