Delphi XE2: How to define custom DataSnap REST URI?
Asked Answered
P

2

10

I am using Delphi XE2 to write DataSnap REST service. I notice that the REST URI in DataSnap must strictly follow this format (refer here):

http://my.site.com/datasnap/rest/URIClassName/URIMethodName[/inputParameter]*

A famous example is sample method create by DataSnap server wizard:

http://my.site.com/datasnap/rest/TServerMethods1/ReverseString/ABC

There are 2 common ways to supply parameters in URI:

  1. Path Segment parameter: /TServerMethods1/ReverseString/ABC
  2. Query String parameter: /TServerMethods1/customers?name=bill

The Path Segment parameter URI is definitely supported by DataSnap REST. Is Query string parameters URI support in DataSnap REST too?

I have the following REST URI example and found it seems impossible to make it work with current DataSnap REST library:

  1. /customers/A1234

    return customer object of ID A1234

  2. /customers/A1234.xml

    return customer object of ID A1234 in XML format

  3. /customers/A1234.json

    return customer object of ID A1234 in json format

  4. /customers/A1234.html

    return customer object of ID A1234 in html format

  5. /customers?name=Bill

    return a list of customer whose name contain Bill

Pettifog answered 12/5, 2012 at 9:17 Comment(0)
T
5

I don't know how to do it using DataSnap, but there are ways around it. You can put something called URLRewrite to good use for this as both your friendly URI's and the ones required by DataSnap are easily mappable.

For IIS you can use (enable) the URLRewrite module which is standard in IIS 7. More information can be found on the official site: http://www.iis.net/download/urlrewrite.

Be sure to create rules for inbound and outbound URI's so that the "internal" (Datasnap) URI's do not get out into the wild.

If you are running the site on Apache, similar functionality is available, and I thin you need to amend the .htaccess file, but I have no experience with Apache so I could be wrong.

Twoseater answered 12/5, 2012 at 9:43 Comment(2)
That would be tedious if the Rest APP is going to deploy on many site.Pettifog
@ChauCheeYang: Why? It's just a couple of rules in web.config. You don't need to use the IIS manager to edit them, you can simply add them straight into the web.config for the site or have an install script do that for you.Twoseater
N
3

A bit late to the party, but yes you can use query parameters.

You have to use GetInvocationMetadata.QueryParams

see the example below.

uses DBXPlatform;

function TServerMethods1.EchoString(Value: string): string;
var
  metaData: TDSInvocationMetadata;
  i: integer;
begin
  metaData := GetInvocationMetadata;
  for i := 0 to Pred(metaData.QueryParams.Count) do
  begin
    Result := Result + '<param>' + metaData.QueryParams[i] + '</param>';
  end;
  metaData.ResponseContent := '<xml>' + Result + '</xml>';
end;
Neoarsphenamine answered 19/2, 2014 at 12:4 Comment(2)
You does not use TDSInvocationMetadata.Create() ... is it correct?Featured
docwiki.embarcadero.com/Libraries/XE2/en/… It is something you yourself should not create, so yes it is correctNeoarsphenamine

© 2022 - 2024 — McMap. All rights reserved.