I have a quick question regarding DataSnap Servers.

I have a quick question regarding DataSnap Servers.
All the documentation I find tell all about how to use the server and connect via Delphi and PHP etc, all using the one JSON param sent in as the data for the request. This is fine for all the program that I write, but in the server that I am current;y woirking on, I need to also respond to callbacks from Twitter and facebook and they pass the parameters in on the URL. I have not been able to find documentaion on how to parse those values out when they call back to my application. I'm sure it's simple and I am over thinking this, but how do I grab that incoming data from the URL?

Thanks,
Jim

Comments

  1. You have the Request and the Response objects plus a Handled var parameters you can set in the WebModuleDefaultAction handler. The tricky thing with this one is that it will be called even for favicon and stuff so you'll need to understand what the "client" is doing.

    If you can get web module actions to correcly respond to the different requests, you can code cleaner by using the same way in each separate web module action's OnAction event.

    I have set the PathInfo to "/index.html" in one of my actions, moved it to the top and did something like this in the OnAction handler:

      business := TBusinessMethods.Create(nil);
      try
        lContent := business.htmlindex(string(Request.Host), Request.ServerPort);
      finally
        business.Free;
      end;

      Response.ContentEncoding := 'iso-8859-1';
      Response.ContentType := 'text/html';
      Response.Title := 'something';
      Response.Content := htmlheaderchunk + lContent + htmlfooterchunk;

    // I need to cover my tracks by stating that i did not fully explore how this is handled thread-wise but is seems to do the job. The creation and freeing of the business object i did like so because this server is invocation only and i can still use the TBusinessMethods.htmlindex function (the same function) the rest-way via javascript and serverfunctions.js //

    You can serve other content types, i added

      if EndsStr('.xml', string(Request.PathInfo)) then
        Response.ContentType := 'text/xml';

    to the WebModuleBeforeDispatch event to be able to server what the requesting part needed as an static xml-file at an address but what i want to serve up dynamically.

    In another slightly different scenario i have my own template with some javascript that will parse the url and call the servermethod with the values as json parameters. In this implementation i set "Response.StatusCode := 404;" in stead of the wizards "Response.SendRedirect(Request.InternalScriptName + '/');". That would depend on what else the server should be doing, of course. I'm pointing this out because it was here that i had to "fiddle" the most to get things to work properly. The wizards implementation is very confusing since it will still serve the request and redirect.

    Also note that response-wise you can access the Request object in the PageProducers OnHTMLTag event and you can call your servermethods from there.

    I would love to have the time to write this in a more concise, proper and informative way but this is what i can do at the moment from the top of my head. Sorry, hope it helps anyway.

    ReplyDelete
  2. I won't be able to play with this until tonight, but this looks great! More information then I have found so far. So, you mention that you 'parse' the URL. I am to assume that you are digging out the param information and that there is no 'clean' method from Delphi that can get you that info. In PHP I have a $_GET['paramname'] and I get the value for that URL param. If something like that doesn't exist, I have no problem parsing the URL myself.

    Thanks for all the great info!

    ReplyDelete
  3. It does look like you have to do that. The Request object has stuff for this, i think.

    The parsing in the project i wrote about is done in the JavaScript part of the template. So i have not had to parse just that in delphi code.



    However, the documentation is very scarce. 

    It was especially very difficult for me to understand how to write the methods in a "best practise" way in order to make them usable in JavaScript, Apps and so forth (the huge non-Delphi world in other words). The mORMot documentation reasons about these things in a glimmering way. I recommend to read/browse some of it, even if you go 100% DataSnap.

    The situation this results in is that the people that you want using your services will choose another service because the response objects are targeted mainly at Delphi clients. There's no reading (that i know of) that discusses if the server method should throw an exception, return an object with a tailored error message or null or what.

    I've written my own JavaScrip these past months and got some insights. But i think that i would not use DS for a "public" service. This is sad. I sat with an iOS programmer i wanted to use my service. I never used a mac or xCode, but i had to run though hoops to set the compiler flags appropriately because of the difference in xCode standard garbage handling and that of the generated headers. This should be clearly documented. The poor guy did not even know to enable debugging, he's still "developing" without debugging (sic) but the free service he found on the net worked for him out of the box. I lost that.

    I suspect we have to wait for XE4 and pay up again (i'm on XE2) to get some of the peculiarities fixed.

    It was at a EMB seminar in Stockholm that i was recommended to upgrade to Enterprise to get DataSnap. Had i not put my trust there (and that was my mistake - the halo effect of the seminar got to me) i could have stayed with Professional and used opensource libraries to achieve the same functionality in a cleaner way. Now i can not afford to update anymore.



    You are welcome to get back to me here if you have more questions. It helps me too because i have to rethink what i've done so far and that's good.

    ReplyDelete

Post a Comment