How can I consume a oData webservice from Delphi (I'm trying to interact with the new Bing Search Azure API)? There is almost no information to do this in Delphi. There is a post here but it does not help much explaining how to consume such service from a Delphi point of view. Can anyone provide a simple example?
Asked
Active
Viewed 3,163 times
1 Answers
4
Here is a very simple example of consuming an oData service in Delphi XE using the netflix oData service:
program oDataDemo;
{$APPTYPE CONSOLE}
uses
SysUtils, msxml, Variants, Activex;
var
httpRequest: IXMLHttpRequest;
oDataServiceURI: String;
oDataFilter: String;
xmlResults: String;
begin
try
oDataServiceURI := 'http://odata.netflix.com/v2/Catalog/Titles()';
oDataFilter := '?$top=10';
coinitialize(nil);
httpRequest := CoXMLHTTP.Create;
httpRequest.open('GET', UTF8Encode(oDataServiceURI + oDataFilter), false, EmptyParam, EmptyParam);
httpRequest.send(EmptyParam);
xmlResults := httpRequest.responseText;
WriteLn(xmlResults);
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.

Mattl
- 1,588
- 3
- 24
- 49
-
Shouldn't it be URLEncode() instead of UTF8Encode(oDataServiceURI + oDataFilter)? – mjn Oct 22 '12 at 18:53
-
1I'll look into URLEncode, my understanding is that a URI should be UTF-8 encoded see this link: http://stackoverflow.com/a/913653/117859 and the section on Current Standard here: http://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding%5Fin%5Fa%5FURI – Mattl Oct 22 '12 at 22:48
-
All the examples with Delphi and rest are of this format. What is the use of the xmlResults as text? If Delphi requires the programmer to manually Parse the Atom pub xml, then Delphi doesn't really support Rest. It just supports HttpRequests. Does XE3 actually address this? – reckface Mar 15 '13 at 11:58
-
1I guess it depends what your RESTful service returns, both XML and JSON are very easy to parse in Delphi. For JSON I usually use a third party library called SuperObject which makes light work of the parsing. – Mattl Mar 16 '13 at 19:48
-
I've not found any simple way to parse odata in Delphi. Even the excellent JSON example given by Remy in https://stackoverflow.com/questions/24815625/parsing-valid-json-with-tjsonobject-using-embarcadero-code-example-fails-with-ex does not work with odata. – SiBrit May 17 '18 at 00:11