Rest api with curl » History » Version 8
Holger Just, 2016-09-20 17:36
same
1 | 1 | Jean-Philippe Lang | h1. Using the REST API with cURL |
---|---|---|---|
2 | |||
3 | "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 | |||
5 | 3 | Jean-Philippe Lang | h2. Using JSON |
6 | |||
7 | 1 | Jean-Philippe Lang | Here is a simple example of a command that can be used to update an issue: |
8 | |||
9 | <pre> |
||
10 | 7 | Holger Just | curl -v -H "Content-Type: application/json" -X PUT --data-binary "@388.json" -u login:password http://redmine/issues/388.json |
11 | curl -v -H "Content-Type: application/json" -X PUT --data-binary "@388.json" -H "X-Redmine-API-Key: xxxx" http://redmine/issues/388.json |
||
12 | 1 | Jean-Philippe Lang | </pre> |
13 | |||
14 | The file that contains the data sent to Redmine (388.json in the example above) would look like this: |
||
15 | |||
16 | <pre> |
||
17 | { |
||
18 | "issue": { |
||
19 | "subject": "subject123", |
||
20 | "notes": "Changing the subject" |
||
21 | } |
||
22 | } |
||
23 | </pre> |
||
24 | 2 | Jean-Philippe Lang | |
25 | 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: |
||
26 | * @application/json@ |
||
27 | 1 | Jean-Philippe Lang | * @application/xml@ |
28 | 3 | Jean-Philippe Lang | |
29 | h2. Using XML |
||
30 | |||
31 | |||
32 | Here is a simple example of a command that can be used to create an issue with custom fields: |
||
33 | |||
34 | <pre> |
||
35 | 7 | Holger Just | curl -v -H "Content-Type: application/xml" -X POST --data-binary "@issue.xml" -u login:password http://redmine/issues.xml |
36 | curl -v -H "Content-Type: application/xml" -X POST --data-binary "@issue.xml" -H "X-Redmine-API-Key: xxxx" http://redmine/issues.xml |
||
37 | 4 | Terence Mill | |
38 | 3 | Jean-Philippe Lang | </pre> |
39 | |||
40 | Where issue.xml content is: |
||
41 | |||
42 | <pre> |
||
43 | <?xml version="1.0" encoding="ISO-8859-1" ?> |
||
44 | <issue> |
||
45 | <subject>API custom fields</subject> |
||
46 | <project_id>1</project_id> |
||
47 | <tracker_id>2</tracker_id> |
||
48 | <custom_fields type="array"> |
||
49 | <custom_field> |
||
50 | <id>2</id> |
||
51 | <value>Fixed</value> |
||
52 | </custom_field> |
||
53 | <custom_field> |
||
54 | <id>1</id> |
||
55 | <value>0.8.2</value> |
||
56 | </custom_field> |
||
57 | </custom_fields> |
||
58 | </issue> |
||
59 | </pre> |
||
60 | 5 | Jean-Philippe Lang | |
61 | 6 | Lucile Quirion | h2. Text formatting |
62 | |||
63 | 7 | Holger Just | If you want to use some text formatting (e.g to update a wiki page on your project), you should make sure to use curl's option @--data-binary@ instead of @--data@ to load the file. Only that way, curl will send newline characters unchanged and will retain all formatting. |
64 | 6 | Lucile Quirion | |
65 | <pre>curl -v -H "Content-Type: application/xml" -X PUT --data-binary "@wiki.xml" -u login:password http://redmine/projects/foo/wiki/page_test.xml</pre> |
||
66 | |||
67 | Where wiki.xml content is: |
||
68 | |||
69 | <pre> |
||
70 | <?xml version="1.0"?> |
||
71 | <wiki_page> |
||
72 | <text> |
||
73 | h1. TITLE |
||
74 | |||
75 | %{font-size:14pt}SUBTITLE% |
||
76 | </text> |
||
77 | </wiki_page> |
||
78 | </pre> |
||
79 | |||
80 | 5 | Jean-Philippe Lang | h2. Attaching files |
81 | |||
82 | If you want to create an issue with image.png attached, you need to upload this file first: |
||
83 | |||
84 | <pre> |
||
85 | curl --data-binary "@image.png" -H "Content-Type: application/octet-stream" -X POST -u login:password http://redmine/uploads.xml |
||
86 | |||
87 | # 201 response |
||
88 | <upload> |
||
89 | <token>7167.ed1ccdb093229ca1bd0b043618d88743</token> |
||
90 | </upload> |
||
91 | </pre> |
||
92 | |||
93 | Then, use the token to create the issue: |
||
94 | |||
95 | <pre> |
||
96 | 8 | Holger Just | curl -v -H "Content-Type: application/xml" -X POST --data-binary "@issue.xml" -u login:password http://redmine/issues.xml |
97 | 5 | Jean-Philippe Lang | </pre> |
98 | |||
99 | Where issue.xml content is: |
||
100 | |||
101 | <pre> |
||
102 | <?xml version="1.0" encoding="ISO-8859-1" ?> |
||
103 | <issue> |
||
104 | <subject>Issue with attachment</subject> |
||
105 | <project_id>1</project_id> |
||
106 | <uploads type="array"> |
||
107 | <upload> |
||
108 | <token>7167.ed1ccdb093229ca1bd0b043618d88743</token> |
||
109 | <filename>image.png</filename> |
||
110 | <content_type>image/png</content_type> |
||
111 | </upload> |
||
112 | </uploads> |
||
113 | </issue> |
||
114 | </pre> |