Jump to content
GenoR

TBackendEndpoint and rmPOST best practices

Recommended Posts

I am new to REST development and have designed a simple Rad Server package with GET/POST and the like. 

I am using a BatchMover and a JSONWriter to fulfil the GET request, but am trying to figure out the best route to process a POST request sent by a client.

I'm new to all of this and kind of self-teaching (with plenty of help online).

 

In my client App I have a TBackendEndpoint object that I am adding parameters to and executing to trigger the post routine in my server module.  Rather than receiving a JSON format I get my parameters concatenated together with &.

 

I could just parse this out in my POST routine, but I am thinking I am doing this all wrong.  I have a few questions

Am I using the right object to carry this out? (namely TBackendEndpoint)

If so, how do I get this to send the Request in JSON format (don't I need to)?

 

Here is the code from the Client side:

//ADDING PARAMETERS TO A REQUEST AND EXECUTING IT

      bkEndEPAddTran.AddParameter('GCKEY','66');
      bkEndEPAddTran.AddParameter('TRANDATE','20221001');
      bkEndEPAddTran.AddParameter('TRANTIME','15:22:08');
      bkEndEPAddTran.AddParameter('TRANLOC','120');
      bkEndEPAddTran.AddParameter('TRANTYPE','1');
      bkEndEPAddTran.AddParameter('TRANAMT','50.00');
      bkEndEPAddTran.AddParameter('USERID','GenoR');
      bkEndEPAddTran.Execute;

 

And the code in the POST method of my Rad Server:

//JUST LOOKING AT THE STRING REQUEST FROM THE CLIENT

    logStr := TStringlist.Create;
    logStr.Add(ARequest.Body.GetString);
    logStr.SaveToFile('postJSON');

 

The postJSON file contains

GCKEY=66&TRANDATE=20221001&TRANTIME=15%3A22%3A08&TRANLOC=120&TRANTYPE=1&TRANAMT=50.00&USERID=GenoR

 

These are just snippets of the routine that is doing the work in question.  Again, I could move forward parsing this result and creating my SQL insert with it, but I want to follow best-practices.

 

Share this post


Link to post

I may have found my own answer : bkEndEPAddTran.Body.JSONWriter

After playing around the the TBackendEndpoint component more it occured to me that the Body might have a JSONWriter since it was of ARequest type and I know that the AResponse type's body had a JSONWriter.  Going to play around with that.  Somebody stop me if I'm going down the road of madness and there is a better way!  Thank you in advance.

Share this post


Link to post

I ended up just manually creating the JSONObject and adding pairs then adding that object to the body of the response.  As long as my request isn't a very large JSON (and it shouldn't be) this is probably fine I'm thinking.

 

procedure TfrmGCClient.btnAddTranClick(Sender: TObject);
var
  JSONObj : TJSONObject;
  i: Integer;
begin
    try
      jsonObj := TJSONObject.Create;

      jsonObj.AddPair('gcKey','66');
      jsonObj.AddPair('TranDate','20221001');
      jsonObj.AddPair('TranTime','15:22:08');
      jsonObj.AddPair('TranLoc','120');
      jsonObj.AddPair('TranType','1');
      jsonObj.AddPair('TranAmt','50.00');
      jsonObj.AddPair('UserID','GenoR');

      bkEndEPAddTran.AddBody(jsonObj);
      bkEndEPAddTran.Execute;

    finally
      jsonObj.Free;
      jsonObj := nil;
    end;
end;

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×