Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can RemObjects SDK parameters be passed via the URI?

We have a RemObjects SDK HTTP server that exposes a number of services and methods. Is it possible to call a method via a URI rather than passing the parameters as SOAP/JSON e.g.

http://www.mywebservice.com/servicename/methodname?param1=xxx&param2=yyy
like image 694
norgepaul Avatar asked Feb 01 '13 11:02

norgepaul


1 Answers

Here's a play on norgepaul's solution that looks good and returns JSON. It's based on the same idea of intercepting the HTTP request using a descendant of TROIndyHTTPServer, but this time I'm not only fixing the parameters of the request, I'm creating the "JSON" post that the client didn't send!

Here's the code that I used to test with the default "VCL Standalon" server implementation:

TUriROIndyHTTPServer = class(TROIndyHTTPServer)
protected
  procedure InternalServerCommandGet(AThread: TIdThreadClass; RequestInfo: TIdHTTPRequestInfo; ResponseInfo: TIdHTTPResponseInfo); override;
end;

procedure TUriROIndyHTTPServer.InternalServerCommandGet(AThread: TIdThreadClass;RequestInfo: TIdHTTPRequestInfo; ResponseInfo: TIdHTTPResponseInfo);
var A, B: Integer;
    NewPost: AnsiString;
begin
  if RequestInfo.Document = '/json/sum' then
    begin
      // Extract the parameters
      A := StrToIntDef(RequestInfo.Params.Values['a'], 0);
      B := StrToIntDef(RequestInfo.Params.Values['b'], 0);
      NewPost := AnsiString(Format('{"version":"1.1","method":"NewService.Sum","params":{"A":"%d","B":"%d"}}', [A, B]));

      // Prepare the (fake) post-stream
      RequestInfo.PostStream.Free;
      RequestInfo.PostStream := TMemoryStream.Create;
      RequestInfo.PostStream.Write(NewPost[1], Length(NewPost));
      RequestInfo.PostStream.Position := 0;
    end
  else if RequestInfo.Document = '/json/getservertime' then
    begin
      // Extract the parameters
      NewPost := '{"version":"1.1","method":"NewService.GetServerTime"}';

      // Prepare the (fake) post-stream
      RequestInfo.PostStream.Free;
      RequestInfo.PostStream := TMemoryStream.Create;
      RequestInfo.PostStream.Write(NewPost[1], Length(NewPost));
      RequestInfo.PostStream.Position := 0;
    end;

  inherited;
end;

With this sort of code in place, I can make requests like this:

http://localhost:8080/json/sum?a=1&b=2

returns (in browser!)

 {"version":"1.1","result":"3"}       

and this:

 http://localhost:8080/json/getservertime

returns this (well, at the time of this writing):

{"version":"1.1","result":"2013-02-01T19:24:24.827"}
  • The whole code for my server's main form pastebin link
  • The DFM for my server's main form pastebin link

The result (in browser or foreign application) is pure JSON because it's been formated as a "JSON Message" using RO's code.

like image 100
Cosmin Prund Avatar answered Oct 30 '22 14:10

Cosmin Prund