Using the REST API with Delphi¶
Here are two simple procedures that demonstrates how to use the Redmine REST API with Delphi. This example uses ICS component THttpCli:
1//Creating an Issue
2procedure AddIssue;
3var
4 xmlStream : TMemoryStream;
5 tempArray : array[0..10000] of char;
6 xmlIssue : String;
7 apiAccessKey : String;
8 HttpClient : THttpCli;
9begin
10 apiAccessKey := 'a1234567abcde12121a11a123456a12a12ab123ab'; ///Api user key example
11 xmlStream := TMemoryStream.Create; //Create the XML stream
12
13 //Create and set the Http Client component
14 HttpClient := THttpCli.Create(nil);
15 HttpClient.ContentTypePost := 'text/xml';
16 HttpClient.URL:= 'http://localhost:3000/issues.xml?key='+apiAccessKey;
17 try
18 xmlIssue :=
19 '<?xml version="1.0"?>' +
20 '<issue>' +
21 ' <project_id>1</project_id>'+
22 ' <tracker_id>1</tracker_id>'+
23 ' <status_id>1</status_id>'+
24 ' <priority_id>1</priority_id>'+
25 ' <author_id>1</author_id>'+
26 ' <start_date>2011-10-09</start_date>'+
27 ' <subject>Created in DELPHI</subject>'+
28 ' <description>Issue created in DELPHI using Redmine REST API</description>'+
29 ' <custom_fields type="array">' + // adding custom fields
30 ' <custom_field id="1">'+
31 ' <value>12121212</value>'+
32 ' </custom_field>'+
33 ' <custom_field id="2">'+
34 ' <value>2010-10-09</value>'+
35 ' </custom_field>'+
36 ' </custom_fields>'+
37 '</issue>';
38
39 // Fill the stream with the xmlIssue
40 FillChar( temparray, SizeOf( temparray ), #0 );
41 strpcopy( temparray, xmlIssue );
42 xmlStream.Write( temparray, length(xmlIssue) );
43 xmlStream.Seek( 0, soFromBeginning);
44
45 // set the xml stream and post
46 HttpClient.SendStream:=xmlStream;
47 try
48 HttpClient.Post;
49 except
50 raise;
51 end;
52 finally
53 xmlStream.Free;
54 HttpClient.Free;
55 end;
56end;
57
58//Listing Issue
59procedure GetIssue(issueId: String);
60var
61 apiAccessKey: String;
62 HttpClient: THttpCli;
63 xmlIssue: TXMLDocument;
64begin
65 Memo.Lines.Clear; //using TMemo to show the result
66 apiAccessKey := 'a1234567abcde12121a11a123456a12a12ab123ab'; //Api user key example
67
68 xmlIssue := TXMLDocument.Create(Self); //Creating XML document. Self = TForm
69
70 //Create and set the Http Client component
71 HttpClient := THttpCli.Create(nil);
72 HttpClient.ContentTypePost := 'text/xml';
73 HttpClient.URL:= 'http://localhost:3000/issues/'+issueId+'.xml?key='+apiAccessKey;
74
75 HttpClient.RcvdStream := TStringStream.Create(''); //Create the Stream
76 try
77 try
78 //Get the Issue and show some properties in TMemo
79 HttpClient.Get;
80 xmlIssue.LoadFromStream(HttpClient.RcvdStream);
81 xmlIssue.Active := True;
82
83 Memo.Lines.Add('Id = ' + xmlIssue.DocumentElement.ChildNodes['id'].NodeValue);
84 Memo.Lines.Add('Project id = ' + xmlIssue.DocumentElement.ChildNodes['project'].AttributeNodes['id'].NodeValue);
85 Memo.Lines.Add('Project Name = ' + xmlIssue.DocumentElement.ChildNodes['project'].AttributeNodes['name'].NodeValue);
86 Memo.Lines.Add('Tracker id = ' + xmlIssue.DocumentElement.ChildNodes['tracker'].AttributeNodes['id'].NodeValue);
87 Memo.Lines.Add('Tracker name = ' + xmlIssue.DocumentElement.ChildNodes['tracker'].AttributeNodes['name'].NodeValue);
88 Except
89 raise;
90 end;
91 finally
92 HttpClient.Free;
93 xmlIssue.Free;
94 end;
95end;